Access custom properties with VBA

Hello - does anybody have VBA sample code showing how to access custom properties in SolidWorks files?

I guess I have to link to a SW-object model and the know how to access the proper values.... but an exanmple would help a lot.

Thanks

Reply to
diane
Loading thread data ...

SW website hase some examples in their sample code i think?

This is what I found to work. If you ensure the old info is deleted, then add a new one with the new text. it is kindof finnicky because it is case sensitive when you delete the old one. But you can't add a new one even if the case is different. Seems silly to me

' you can get the value of the old custom info like this. DescTest = CustProps.Part.GetCustomInfoValue("", "DESCRIPTION")

'delete the old field and add a new one. You may have to make more than one delete line if some of your documents already have the same field in them. Because the Case wasn't regulated.

retval = SetPart.DeleteCustomInfo2("", "Description") retval = SetPart.AddCustomInfo3("", "Description", 30, Description)

Otherwise there are some examples out there. I just don't have the right links.

Reply to
Corey Scheich

After seeing a demo of SWX2004 and finding that SWX still have not provided a better method of handling Properties, I thought I would do something I wanted to do for a while. You may be interested in having a look at the program I wrote, I only finished it to this stage last night.

I have been working on a program to enable easier handling of SolidWorks Properties. There is no proper help file as most of the workings are reasonably self explanatory.

I plan to be send it out free of charge but I give people the option of sending something in if the wish.

Please email me if you want to try it. The file is an standalone .EXE file. so if you have a firewall or protection against .EXE files you may need to allow this through. By all means Virus check it, but it is clear according to NOD32.

Regards Tony O'Hara Ph: 61 3 9799 1264 Mbl: 61 0438 377 283 snipped-for-privacy@bigpond.com Melbourne. Australia.

Reply to
Tony O'Hara

Hi Corey If you could be a little more specific...(show more sample code - of what you found to work)? How do I specify the SolidWorks file to access for properties? What do I link to, to get the object model? Diane

Reply to
Joan

Hi Tony Thanks...but I need it even more specific...

- how do I define the objects?

- what do I link to in my VBA-project to get the object definitions - or do I make a special statement in the code to get that? I would normaly go to Tools - references and then check the relevant dll or activeX etc. I use the VBA in MS-Access2000.

I understand tha complexity of a tool with menus etc - but for now I am just interested in a small application that gets a "hard coded" file name and then retrieves some custom properties. When I have the "connection", I can refine the program my self - with more properties, a user interface etc.

I hope you can help me.

Diane

Reply to
diane

In Access VBA, you can use the References to select the Sldworks.tlb file for early binding or CreateObject to attach to the SolidWorks session object or GetObject.

Hope this helps,

-- Bob Hanson CEO Custom Programming Unlimited LLC SolidWorks Solution Partner in two categories (SW API training/consulting and SW PDM provider)

formatting link
website (Home of SW API Public Code snippets)

SolidWorks 2003 World User Conference Exhibitor Booth #407

Reply to
Robert V. Hanson

This works for me. I have a form that I have the option of typing in a part name or not. If the part name isn't typed in it grabs the active document. If one is it makes that part model current and sets the custom properties for that model and saves when going back to the Model you started on. If you want I could send you the whole program. Also this is set up to have a

1 and 2 line description. I found that I wanted a 1 line description for BOMs and a 2 line for drawing titles. So you type it all into one field and this breaks it up.

Private Sub cmdSetSW_Click()

Dim FName1 As String Dim FName2 As String Dim FName3 As String Dim Description As String Dim Description2 As String Dim returnPos As Long Dim SetPart As SldWorks.ModelDoc2

Dim FileAttr As String Dim CADObject As Object Dim FileExists As String Dim Response

FileExists = "" ' the next few lines are to set the values on your form to variables returnPos = InStr(1, CustProps.txtDescription.text, Chr(vbKeyReturn), vbTextCompare) If returnPos > 0 Then Description = VBA.Left(CustProps.txtDescription.text, returnPos - 1) Else Description = CustProps.txtDescription.text End If Description2 = CustProps.txtDescription.text

'Description = CustProps.txtDescription.text MaterialNo = CustProps.txtMaterial.text

Set swApp = Application.SldWorks Set SetPart = Nothing

'if you set a value to FName1 and it is not current already this will make it current if it is already opened. FName1 = "My Part" Set SetPart = swApp.ActivateDoc2(FName1, True, longstatus)

If SetPart Is Nothing Then ' if FName1 was not set then grab the active document Set SetPart = swApp.ActiveDoc End If

partTitle = SetPart.GetTitle 'Get the title of the document.

If Description = "" Then 'do nothing Else 'delete the old value of Description and set the new one retval = SetPart.DeleteCustomInfo2("", "Description") retval = SetPart.DeleteCustomInfo2("", "DESCRIPTION") retval = SetPart.AddCustomInfo3("", "Description", 30, Description) 'these lines were because some of our parts had the description on the default configuration retval = SetPart.DeleteCustomInfo2("Default", "Description") retval = SetPart.DeleteCustomInfo2("Default", "DESCRIPTION") retval = SetPart.AddCustomInfo3("Default", "Description", 30, Description) End If If MaterialNo = "" Then Else 'set material property same as description retval = SetPart.DeleteCustomInfo2("", "Material#") retval = SetPart.DeleteCustomInfo2("", "MATERIAL#") retval = SetPart.AddCustomInfo3("", "Material#", 30, MaterialNo) retval = SetPart.DeleteCustomInfo2("Default", "Material#") retval = SetPart.DeleteCustomInfo2("Default", "MATERIAL#") End If If Description2 = "" Then 'Nothing Else 'set the 2 line description retval = SetPart.DeleteCustomInfo2("", "Description2") retval = SetPart.AddCustomInfo3("", "Description2", 30, Description2) End If

Check if the current drawing is the one you started in. If not save it and return to the origional If partTitle = dwgTitle Then

Else retval = SetPart.Save3(0, longstatus, longstatus) swApp.CloseDoc partTitle End If Set SetPart = swApp.ActivateDoc2(dwgTitle, True, longstatus)

If CustProps.ResetMatDesc.Value = True Then MaterialText.MtlDesc End If

'I don't think I ever figured out how to rebuild it right. SetPart.Rebuild 1 SetPart.Rebuild 3 End Sub

Reply to
Corey Scheich

I think I'm getting closer... thanks!

When using your sample code in VBA (Access 2000), I get a compile error in the statement

Set swApp = Application.SldWorks

"Method or data member not found" refering to the .SldWorks method of Application

I have checked the "SldWorks 2003 Type Library" in references (which makes the "as ModelDoc2" work).... I have also tried including the "SolidWorks exposed type libraries for add-in use" and SolidWorks Extensibility Type Library" - but without success.

Any suggestions?

Diane

Reply to
Diane

Diane Do you want to see the program I wrote? just to see what can be done. I didn't want to send it unless you want to have a look. It is a small .EXE file and runs outside of Solidworks.

I know there is more than can be done, such as routines, copying entries from one config to another and renaming Configs, but this will come in later versions.

-- Tony O'Hara Melbourne, Australia.

Reply to
Tony O'Hara

Diane

Check out this new macro. This macro will allow you to edit custom file properties and configuration specific properties. Check 'All configurations' to access all configuration specific properties for all configurations in the model. This is written as a SolidWorks '.swp' macro. You can view the code and how it works.

formatting link
Lenny

Reply to
Leonard Kikstra

PolyTech Forum website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.