Archive for the ‘API’ Category

DSOFile beaten by SolidWorks Document Manager

Monday, January 9th, 2012

Hey macro lovers! Have to change a lot of SolidWorks file properties, but don’t want to your code to have to open each file one at a time?

Most web searches are going to point you to use Microsoft’s DSOFile. It’s a good solution, but using it means you need to install it on your client machines (and watch out there is a 32 and 64 bit version out there!) or create a release package. Neither option is very fun if you are just writing a quick SolidWorks macro.

Luckily, the SolidWorks Document Manager is a great option. It gives you write access to many properties inside of a SolidWorks file.

Add “SwDocumentMgr 20XX Type Library” to the list of your macro’s references and your wish is the Document Manager’s command.

refs

IMHO, the documentation is a very “tedious” so here is a little VBA snippet showing how easy this COM object is. This snippet will add the custom property “Material” to the file and assign it the value “Adamantium”:

Dim swDoc As SwDMDocument12
Dim swDM As SwDMApplication
Dim mOpenErrors As SwDmDocumentOpenError
Dim objClassfac As SwDMClassFactory
Set objClassfac = CreateObject("SwDocumentMgr.SwDMClassFactory")
Set swDM = objClassfac.GetApplication(<This is a string/key that you need to request from SolidWorks>)
Set swDoc = swDM.GetDocument(strDocFileName, SwDmDocumentType.swDmDocumentPart, False, mOpenErrors)
swDoc.AddCustomProperty "Material", SwDmCustomInfoType.swDmCustomInfoText, "Adamantium"
swDoc.Save
swDoc.CloseDoc

Just as easy as DSOFile, and more powerful -if you are wanting to update configuration specific properties, DSOFile is going to let you down.

DSOFile is still the best tool for working with file properties of other OLE type (read: Office) documents but when it comes to SolidWorks documents, all the cool kids are using the SolidWorks Document Manager.

By the way, if you are looking for a way to impress your date, read more about the Document Manager. It gives you more control over your SolidWorks files than just messing with file properties. With it you can write applications that emulate file management tools to perform such tasks as renaming, replacing, and copying SolidWorks documents -you don’t even have to have SolidWorks installed!

Jeff Sweeney

Jeff Sweeney
Engineering Data Specialist
3DVision Technologies

Backing up a revision

Wednesday, November 23rd, 2011

When you are the Data Specialist Man, the world is your stage.

Here is a little excerpt from a screen play I am writing for an upcoming movie, tentatively titled: “SolidWorks Enterprise PDM – The Movie”

The scene opens with our hero [me] teaching an EPDM administrator training class.

[Me] Now that the file is in your “Released” workflow state, it is read only for everyone, the only way it can be changed is to push the file through the workflow again, making a new revision.

[Future Admin] But what if I want to change the file without bumping the revision?

[Me] You can’t. That revision has been approved, it shouldn’t be changed.

[Future Admin] But I want to.

[Me] No, that would be like changing history. What’s the big deal? Revision letters are cheap, bump the revision and go on with your life.

[Future Admin] But I want to.

[Me] No! You just just bought EPDM to protect your documents and now, on the very first day, you want to override it?!

[Future Admin] Yes.

[Me] Fine. However, I insist we still make everything traceable so we still have record of the actual version that was originally approved.

[Future Admin] Deal.

[Me] First, we’ll make what I like to call a relief valve in our workflow:

ReliefValve

The “Released” state will stay read only for everyone. We’ll only give permission for the administrator to use the “Override Revision” transition. We’ll only give checkout rights in the “Revision Override” workflow state to a very few trust worthy people. They can modify the file, then re-release it.

[Future Admin] But now won’t there be additional versions after the revision?

[Me] Say! I didn’t think you were paying attention while you were playing with your cell phone! Yes, there will be an additional version. However with EPDM’s API, we can actually roll back the revision number component to any value we want. Then reapply the revision.

History

By looking at the file’s history we can see that version two was approved as revision “A”, then version three was created, which was also approved as revision “A”. This second approval was done by a little API application that rolled the counter back one place.

Now the entire process was recorded in the file’s history and you still get to have your changed file at revision “A”.

[Future Admin] Thank you Engineering Data Specialist Man! How can we ever repay you?!

[Me] <Laughs> All in a day’s work my friend, all in a day’s work.

Curtain.

Fin.

Jeff Sweeney

Jeff Sweeney
Engineering Data Specialist
3DVision Technologies

How to search for every file owned by any user in Workgroup

Friday, November 18th, 2011

Recently a customer asked me to write a routine to release ownership of every file in his SolidWorks Workgroup PDM vault.

I thought finding the files would be rather simple until I realized Workgroup’s PDMWSearchOptions (Workgroup’s API search function) does not support wildcard characters. While contemplating harakiri, I finally came up with this search criteria idea:

objSearch.IgnoreCase = True
objSearch.SearchCriteria.AddCriteria pdmwOr, pdmwOwner, "", pdmwContains, "a"
objSearch.SearchCriteria.AddCriteria pdmwOr, pdmwOwner, "", pdmwContains, "e"
objSearch.SearchCriteria.AddCriteria pdmwOr, pdmwOwner, "", pdmwContains, "i"
objSearch.SearchCriteria.AddCriteria pdmwOr, pdmwOwner, "", pdmwContains, "o"
objSearch.SearchCriteria.AddCriteria pdmwOr, pdmwOwner, "", pdmwContains, "u"
'sometimes
objSearch.SearchCriteria.AddCriteria pdmwOr, pdmwOwner, "", pdmwContains, "y"

As long as I don’t have any Workgroup users from one of those countries that do not have any vowels in their names, this search gave me every file in the Workgroup vault that was owned by a user. It isn’t a lightening fast search, but did a nice job.

Jeff Sweeney

Jeff Sweeney
Engineering Data Specialist
3DVision Technologies

LoadDraftingStandard

Friday, August 12th, 2011

Imagine you received a set of SolidWorks drawings from an outside source. Unfortunately the people who created the drawings didn’t use your drafting standards. [The peasants!]

You may be tempted to create a little macro that goes through every one of your settings, one at a time, setting user preference toggles, integer values, blah, blah… Not a bad idea, but doing this is slow, tedious and you’ll run into some “undocumented issues” that my lawyers don’t want me to get into. So instead, open your drawing template and save your standards out as a file.

ExternalFile

Now in your API code, after you open the drawing, use the “Drawing.Extension.LoadDraftingStandard” method to load the standards file you created. This one line of code can then take place of the possible hundreds of lines of codes setting the preferences one at a time.

Jeff Sweeney

Jeff Sweeney
Engineering Data Specialist
3DVision Technologies

Print your dwg files with EPDM Tasks

Monday, April 18th, 2011

Would you like to have your SolidWorks Enterprise PDM 2011 tasks be able to [batch] print your dwg files as well as your SolidWorks drawings?

It is pretty easy, you only need to make a few changes to your Print Task’s “Advanced Scripting Options”.

Click on the advanced scripting options button in the bottom of the “Files” section.

scriptingbutton
Now all you have to do is find the function “bIsSupportedExtension” and replace all of it with this code:
Function bIsSupportedExtension(oExtension) As Boolean
oExtension=lcase(oExtension)
If oExtension = "prt" Then
bIsSupportedExtension = False
ElseIf oExtension = "asm" Then
bIsSupportedExtension = False
ElseIf oExtension = "drw" Then
bIsSupportedExtension = False
ElseIf oExtension = "dxf" Then
bIsSupportedExtension = False
ElseIf oExtension = "dwg" Then
bIsSupportedExtension = True
ElseIf oExtension = "psd" Then
bIsSupportedExtension = False
ElseIf oExtension = "ai" Then
bIsSupportedExtension = False
ElseIf oExtension = "lfp" Then
bIsSupportedExtension = False
ElseIf oExtension = "sldlfp" Then
bIsSupportedExtension = False
ElseIf oExtension = "prtdot" Then
bIsSupportedExtension = False
ElseIf oExtension = "asmdot" Then
bIsSupportedExtension = False
ElseIf oExtension = "drwdot" Then
bIsSupportedExtension = False
ElseIf oExtension = "x_t" Then
bIsSupportedExtension = False
ElseIf oExtension = "x_b" Then
bIsSupportedExtension = False
ElseIf oExtension = "xmt_txt" Then
bIsSupportedExtension = False
ElseIf oExtension = "xmt_bin" Then
bIsSupportedExtension = False
ElseIf oExtension = "igs" Then
bIsSupportedExtension = False
ElseIf oExtension = "iges" Then
bIsSupportedExtension = False
ElseIf oExtension = "step" Then
bIsSupportedExtension = False
ElseIf oExtension = "stp" Then
bIsSupportedExtension = False
ElseIf oExtension = "sat" Then
bIsSupportedExtension = False
ElseIf oExtension = "vda" Then
bIsSupportedExtension = False
ElseIf oExtension = "wrl" Then
bIsSupportedExtension = False
ElseIf oExtension = "stl" Then
bIsSupportedExtension = False
ElseIf oExtension = "cgr" Then
bIsSupportedExtension = False
ElseIf oExtension = "wrl" Then
bIsSupportedExtension = False
ElseIf oExtension = "xpr" Then
bIsSupportedExtension = False
ElseIf oExtension = "xas" Then
bIsSupportedExtension = False
ElseIf oExtension = "ipt" Then
bIsSupportedExtension = False
ElseIf oExtension = "iam" Then
bIsSupportedExtension = False
ElseIf oExtension = "par" Then
bIsSupportedExtension = False
ElseIf oExtension = "psm" Then
bIsSupportedExtension = False
ElseIf oExtension = "ckd" Then
bIsSupportedExtension = False
ElseIf oExtension = "emn" Then
bIsSupportedExtension = False
ElseIf oExtension = "brd" Then
bIsSupportedExtension = False
ElseIf oExtension = "bdf" Then
bIsSupportedExtension = False
ElseIf oExtension = "idb" Then
bIsSupportedExtension = False
ElseIf oExtension = "3dm" Then
bIsSupportedExtension = False
Else
bIsSupportedExtension = False
End If
End Function

Lastly, if you wish to have all the prints “Scaled to fit” (regardless of file type) -add this highlighted line in the location shown:

PrintToFit

[Tested on EPDM 2011 SP 2.0.]

Jeff Sweeney

Jeff Sweeney
Engineering Data Specialist
3DVision Technologies

Use Dispatch to manage on-the-fly workflows

Tuesday, April 12th, 2011

Last week Lou Gallo and I recorded a pod cast discussing SolidWorks Enterprise PDM’s dispatch “script” language. I can’t add much more to the “cool-ness” of dispatch than Lou and I mentioned in the pod cast, but since the recording I have thought of another nice dispatch application I have seen…

SolidWorks has a nice knowledge base article (S-014655) on how you can use a parallel workflow in instances where you need more than one person [group] to be able to approve a document at once.

parallelThis image is a simple example as discussed in S-014655, the concept is -as users push the document through their “voting” transitions, their vote is recorded on the data card. Then via conditions of the transitions, the file either stays and waits for others to vote, or once the file gets all the votes it needs, it then can continue through the life cycle.

The limitation of this example is that all files in this workflow must be approved by all three voters. What if you want the ability to allow your users to determine who needs to vote?

Assuming you have something similar to SolidWorks’ example above, you have some controls that are used to track how the voting is going. Likely you have these “voting” controls read only to keep people from voting for others.

votes

To make this work, you need to add additional controls to give your users the ability to determine who needs to vote. Something like this:

requiredvotes

Here, our user has determined that only “B” and “C” need to vote for this file, so I can ask dispatch to vote for “A” by proxy. (If we don’t get a vote for “A” somehow, the file will not be able to pass through the workflow as designed.)

Next, install dispatch and create a new script.

The concept of the script is pretty close to Lou’s EPDM Dispatch Jumpin’ blog entry. Get the data card variable values and assign them to variable names inside of your dispatch routine.
SetVars

Set the dispatch to run as the file goes through a chosen transistion:

activation

The dispatch script would look like this: (Don’t worry about some of the weird syntax in the “Content” column, the dispatch wizard will build this for you.)

Dispatch

First line tells dispatch to check out the file. (Even the mighty dispatch cannot modify a card without it being checked out.)

The second line says that if person “A” needs to vote (the variable “varAVote” would equal 1 because a checked checkbox is equal to one) jump to the fourth line because I don’t want my dispatch routine to proxy for “A”. However if “A” does not need to vote, the value of varAVote would equal 0 and thus dispatch will simply continue to the third line of the macro.

The third line sets the datacard variable “Vote from A” to 1 – essentially checking the vote check box.

setvarincard

This works even if the control [checkbox] is read-only!

This logic continues for every person that needs to vote, then when we get to the end the file gets checked back in. Don’t worry if everyone is voting and dispatch didn’t change anything. If nothing was changed, the file is “undo check out”ed.

Thus my new file looks like this afterwards:

afterdispatch

I hope I didn’t scare you off, it really is quite easy. As I mentioned, you can think of the dispatch editor as a big wizard, it leads you through the process, it is easier than I made it look.

Jeff Sweeney

Jeff Sweeney
Engineering Data Specialist
3DVision Technologies

Macro Anyone?

Monday, March 21st, 2011

Last week at the Cincinnati User Group Meeting, an engineer from Oystar North America said that he downloaded a cool macro that would create multiple Isometric views.  I was able to find a site that has this macro plus others that you may find interesting.  The site is Lorono’s SolidWorks Resources

Always remember that when you are using a macro, be sure to test it out before using it in production.

Josh Spencer

Josh Spencer
Application Engineer
3DVision Technologies

3DVision’s SWW Presentations

Thursday, February 3rd, 2011

Didn’t get to make the trip to San Antonio to see our SolidWorks World presentations live?  Or maybe you did, but you loved them so much you want to watch them over and over again like Cosby episodes?  Well, either way, you’re in luck because all the presentations have been posted along with the file sets that went with them right here:

http://www.3dvision.com/swwpresentations

Enjoy!

Jordan Tadic
Application Engineer, CSWE
3DVision Technologies
follow me @TadicWorks

“Compile Error: Object Library Feature not Supported” in SolidWorks Macro

Thursday, January 13th, 2011

 After nearly two years at 3DVision I am being released into the CAD blogosphere.  Management was wary but finally caved.  Jeff Sweeney is jealous that he no longer has a monopoly over the API category.  Diversity is good, however, and so as to distinguish myself from him and rest of the 3DVision tribe I will mostly focus on SolidWorks administration, installation, and performance troubleshooting, with the occasional API post thrown in for seasoning.  I can hear you adding this to your RSS feeds already!

Let’s kick things off with the API.  As you know, or as the weird guy a couple cubes over has insisted, the SolidWorks API is pretty cool.  Balancing out this coolness, unfortunately, are some very peculiar headaches.  Perhaps the most common of these headaches is the sudden “Compile error” warnings you might get when running a macro that caused you no trouble in the past.  Out of the blue, this is what you might see:

Compile error: Can't find project or library

Other variations might include “Object library feature not supported”.  What is particularly strange is that the problematic code is often times something as innocuous as a string manipulation function, such as Left().  Otherwise the problem is probably with the first line of your main sub-procedure: “Set swApp = Application.SldWorks”.

Though this can arise out of the blue, sometimes the error occurs when you upgrade to a new operating system (e.g., Win XP to Win 7).  Regardless, the cause is the same: the macro has “lost touch” with its references.  Why?  I would probably have to crawl around MSDN knowledge base articles to find that out, something I am averse to doing.  The solution, however is simple: reset the references.  One way to do this, while in the VBA IDE is to go to Tools–>References, and uncheck any SolidWorks-related references, click OK, re-open the References, re-check those same references, and click OK.  (Sometimes you might also see a reference with “MISSING” in front of it, which can simply be unchecked.) 

references

If this still fails, the issue  might be with one of the primary VBA libraries that you cannot uncheck while in the macro (you’ll get a “Can’t remove control or reference; in use” warning).  In this case, you will need to create a brand new macro and copy and paste your code from your old macro into this new one.  This is the foolproof solution, but can be tedious if you need to move forms as well.

Bonus: on Windows x64 there is another “bug” that is equally frustrating.  If your VBA macro has forms that pop up during the execution, fair chance that they will pop up BEHIND the SolidWorks application, leaving the end user scratching his head as to why SolidWorks suddenly froze.  But it did not freeze at all!  Simply hit Alt+Tab and scroll through the available windows until you find this form.   S-013887 in the SolidWorks knowledge base includes code that can be added to your macro to fix this, however, in my experience, adding the extra code is tedious and adds enough complexity to your code that you might as well get used to Alt+Tab on less-used macros.

Happy coding,

Keith Rice

Keith Rice, CSWP
Application Support Engineer
3DVision Technologies

Auto Update Word Fields

Thursday, October 14th, 2010

SolidWorks Enterprise PDM’s datacards make it easy for you to put information from the datacard directly into your Microsoft Word document. Simply connect the datacard’s variable to the Word’s custom file property, then link a Word field to that custom property.

The problem is that Word does not automatically update fields when you open the file. (Excel will, why won’t Word?!) I know many people use some API code to automatically update the fields every time the document is opened (there is some sample code in the administration manual showing you exactly how to do this) but what if you don’t like to use API?

Wouldn’t it be nice if Word had a setting to automatically update the fields before you save the document? Unfortunately, as far as I can tell it doesn’t….but I may have the next best thing. In Word 2007 go to “Word Options” WordOptions then at the bottom of the display section you should see:
before printing

[For Word 2003 look in the Tools menu, click Options, and then click the Print tab. Hopefully someone out there can tell me where it is for Word 2010.] At least with this method all hard copies should be correct.

Going back to the API solution. If you put the field update code mentioned above in your “Normal.dotm” file, even files you created in the past will begin to automatically update –and as a bonus you will not be bothered to “Enable Macros” every time you open your documents.

Jeff Sweeney

Jeff Sweeney
Engineering Data Specialist
3DVision Technologies

WordPress SEO fine-tune by Meta SEO Pack from Poradnik Webmastera