Archive for the ‘API’ Category

VBMsgeBoxResult

Thursday, May 9th, 2013

Most people only use Visual Basic’s Message Box as an “alert” tool – a way to pass information to the user. Did you know you could use it to pass information back to your macro?

The SolidWorks API class teaches you to declare a variable and set that variable equal to the value returned by MsgBox.

integer

Works great, now the button the user chooses (in this case either a “Yes” or “No” button (most likely in this case “Yes”)) will be stored in the “result” variable.

…but how do you know the values for Yes, No, Cancel, OK? … so you can compare “result” later in your code? I never remember all the different return values for MsgBox, and the help file is too slow. So instead of declaring the variable as a simple integer, I always declare it as the “VbMsgBoxResult” enumerator. Now, any time I access that variable, Visual Basic gives you some nice IntelliSense.

As soon as I click the equals sign, the IntelliSense appears.

vbmesgboxresult

This works for .NET programming too except you declare the variable as “MsgBoxResult” instead of “VbMsgBoxResult”

SetVar works with checked in files

Friday, October 5th, 2012

My favorite new function in SolidWorks Enterprise PDM 2013 -by far, is version free variables. (Special variables that can be updated on a datacard without having to bump the version of the file.) I’ll certainly be talking more about them later, but I just finished a quick test and couldn’t wait to tell you what I learned.

I was initially worried because there is nothing in the API “What’s New” document mentioning any new functionality modifying version free variables. Turns out we don’t need any new commands. SetVar will work even if the file is checked in. Couldn’t be easier.

In EPDM 2013, check in “objFile”, and this code still works!

varEnum = objFile.GetEnumeratorVariable
varEnum.SetVar("VersionFreeVar", "", "Jeff Is Cool")
varEnum.CloseFile(True)

Interview with CADSharp

Thursday, September 13th, 2012

I don’t accept as many interview requests as I used to, but when Keith Rice from CADSharp contacted my publicist about doing an interview with him, I quickly sent my driver to deliver him to the ranch so we could meet. (I find the interview room much nicer in the ranch than over in the main estate.)

Keith and I used to work together when he was here at 3DVision. Since we share many common interests we quickly became friends, thus I was sad when just over a year ago Keith left and started CADSharp. None of us heard much from him as he holed himself in his lab building the web site -until this February at SolidWorks World when the site was released.

I really like the way the site is set up, he has videos to help you learn the basics of SolidWorks API programming and a very big library of well documented macro examples. A programmer never writes anything from scratch, so it is easy to pull macros straight from CADSharp into your projects.

I hope you enjoy reading the interview: http://www.cadsharp.com/blog/interview-api-expert-jeff-sweeney/

Auto Version Increment

Thursday, August 30th, 2012

Today’s SolidWorks Enterprise PDM post comes from a cool little API idea Lou Gallo dropped on me:

poInfo.mlAddInVersion = Now.ToString("yyyyMMdd")

Just like the fight club, if I have to explain it, you wouldn’t understand.

Restricting your EPDM addin

Sunday, April 15th, 2012

There are a lot of API snobs out there who feel their SolidWorks Enterprise PDM code is too good to be run by just anyone, perhaps only members of a certain user group are allowed to run their precious code.

Perhaps they are writing something that should only be run by administrators.

I personally am a programmer for the people; but if you are related to Marie Antoinette, it is pretty easy to accomplish this act of discrimation. You include an “if-then” statement, like the one below, in your addin’s “GetAddInInfo” routine and you’ll be eating cake in no time.

If AllowedUser(poVault) = True Then
poCmdMgr.AddCmd(1, "Name of the special function", EdmMenuFlags....)
End If

You’ll need to write an “AllowedUser” function that can use the vault object parameter to return TRUE if this person is royalty or FALSE if this is a “commoner”.

Commoners will never even see your routine in their right mouse click.

Remember the “GetAddInInfo” routine is only run once – when you first log into the vault. If a user suddenly becomes an “allowed” user, he’ll have to reload his Windows Explorer process to see the change.

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!

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.

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.

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.

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.]

Bear
Bear