Advanced API: Storing Data in Models Using Attributes

Blog 5.30.2012 2 Comments

SolidWorks API attributes
An attribute is a container of user-defined data that a SolidWorks API programmer can store on a SolidWorks model or entity. Like custom properties, they are saved with the model. Attributes, however, have three notable advantages over custom properties:

  • A single attribute can contain an unlimited number of parameters of different types
  • Attributes can be associated with specific geometry (e.g., faces, edges, vertices) and therefore traversed
  • Attributes can be visible to the end user OR hidden

Programming attributes involves three interfaces: IAttributeDef, IAttribute, and IParameter. Here are the basic steps for creating an attribute.

  1. Define the attribute using ISldWorks::DefineAttribute, which returns the IAttributeDef object. One argument is required—a unique definition name.
  2. Add any number of parameters to the definition using IAttributeDef::AddParameter.
  3. Register the definition using IAttributeDef::Register.
  4. Create an instance of the definition on the model or on a model entity using IAttributeDef::CreateInstance5, which returns an IAttribute object. If the attribute is being created on the model, use Nothing for the second argument, otherwise specify the entity (e.g., an IFace2 or IEdge object).
  5. Optionally, get the parameters for that attribute using IAttribute::GetParameter.
  6. Optionally, get or set the parameter value using IParameter::GetDoubleValue / SetDoubleValue2 (if the parameter is of type double) or IParameter::GetStringValue / SetStringValue2 (if the parameter is of type string).

If you wanted to add an attribute called “MyAtt” containing a double value of 0.1 to a selected face, your code would look like this:

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swSelMgr As SldWorks.SelectionMgr
Dim swFace As SldWorks.Face2
Dim swAttDef As SldWorks.AttributeDef
Dim swAtt As SldWorks.Attribute
Dim swParam As SldWorks.Parameter
Sub main()
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swSelMgr = swModel.SelectionManager
    Set swFace = swSelMgr.GetSelectedObject6(1, -1)
    Set swAttDef = swApp.DefineAttribute("template")
    swAttDef.AddParameter "area", swParamTypeDouble, 0.1, 0
    swAttDef.Register
    Set swAtt = swAttDef.CreateInstance5(swModel, swFace, "MyAtt", 0, swAllConfiguration)
    Set swParam = swAtt.GetParameter("area")
    swParam.SetDoubleValue2 swFace.GetArea, swAllConfiguration, Empty
End Sub

While this may seem a little complicated, keep in mind the tremendous versatility available to us. If we want, we could keep using our attribute definition over and over as we create multiple instances on different faces. Multiple instances of the same attribute definition can be created on the same entity, however each instance must have its own name.

Reading the values from existing attributes can be done many different ways. Since an attribute instance is a feature, it can be traversed using normal feature traversal techniques, accessed directly using FeatureByName (available in IPartDoc, IAssemblyDoc, and IDrawingDoc), or, if it is selected in the FeatureManager tree, it can be accessed using ISelectionManager::GetSelectedObject6. Once the IFeature object is obtained, use IFeature::GetSpecificFeature2 to get the actual IAttribute object.

If the attribute is associated with a particular piece of geometry, the aforementioned techniques might not be helpful. Instead, while you traverse your geometry, you can use IEntity::FindAttribute to determine if an attribute of a particular name is present. In this case, be sure to call IAttribute::GetEntityState to make sure that the instance is valid.

In our Macro Library we have to two great examples that demonstrate how to add, find, and read attributes:
Add and read attributes on document (Free)
Add or find attributes on all faces (Premium)

Finally, some words of caution/advice when using attributes:

  • It is not recommended that you use attributes solely for located entities that, for example, need to be mated. There are far better techniques for locating topology / geometry. Use attributes only store data within the model.
  • Attributes can be named anything a programmer desires. However, since other third party applications can use attributes, it is recommended that add-in programmers name their attributes with a unique three character prefix. Send attribute prefix requests to apisupport@solidworks.com.
  • Use double parameters instead of integer parameters, because there is no way to get or set the latter. (See the API Help article IAttributeDef::AddParameter.) Also note that only double parameters can have a default value set. For string values, use Empty in the third argument of IAttributeDef::AddParameter and set the value later using IParameter::SetStringValue2.
  • Once an attribute definition is registered, it cannot be modified. For example, you cannot add, remove, or change the names of parameters. Check the return value of IAttributeDef::Register to see if registry is failing. You may have already registered a definition with that name.
  • The only time attributes can be added to a model while a PropertyManager page is open is in the AfterClose event, since adding an attribute is adding a feature, and adding a feature while a PMP is active is not allowed (or at least will typically not work).
  • If you are running into errors like Run-time error 91 but aren’t sure why, try restarting SolidWorks to give yourself a clean slate in terms of memory.

That concludes our look at attributes. Thanks for reading!

Want to keep up with new CADSharp.com content? Sign up for our newsletter.


Leave Comment

Workgroup PDM API Resources

Blog 5.21.2012 4 Comments


Enterprise PDM is, no doubt, SolidWorks Corp’s flagship PDM package. This has been the case already for a few years. That being said, Workgroup PDM is not dead and remains a great data management solution for small to mid-sized companies. Savvy CAD managers will still want to use the Workgroup PDM API, and for that reason I am pleased to present to you some WPDM API resources I have collected over the years.

First, let’s review how to create a very simple WPDM macro using VBA. This code simply saves out revision “A-02+” of part “block.sldprt” from the vault to “C:\Shop Floor”. You will see that I am using the default WPDM user name and password. The vault is located on a server with DNS name “dataserver”. Also note that before this code will run you must have the “SolidWorks Workgroup PDM 1.0 Type Library” referenced under Tools –> References (if using VBA).

And now the code.

Dim myPDMConn As New PDMWConnection
Dim myDoc As PDMWDocument
Dim strDocName As String
Sub Main()
    Set myPDMConn = CreateObject("PDMWorks.PDMWConnection")
    myPDMConn.Login "pdmwadmin", "pdmwadmin", "dataserver"
    myPDMConn.Refresh
    strDocName = "block.sldprt"
    Set myDoc = myPDMConn.GetSpecificDocument(strDocName, "A-02+")
    myDoc.Save "C:\Shop Floor"
    myPDMConn.Logout
End Sub

As you can see, the API calls are fairly straightforward. If you want to see what else is available in the WPDM API then just go to the local API Help and under the top level of the Contents tab look for “SolidWorks Workgroup PDM API Help”. For the online version go here. Some of the more notable capabilities of the WPDM API include:

  • Checking data in and out
  • Saving from the vault
  • Querying the vault
  • Export queried data to Excel, Access, SQL Server, or Oracle
  • Triggers (notifications)
  • Store trigger information in a database

A great PDF that covers all of this was written by Jerry Winters as part of an excellent presentation delivered at SolidWorks World 2008 called “Making the most of the PDMWorks API”. Most of the examples are in VB.NET, by the way.

Another notable WPDM API presentation was delivered at SolidWorks World 2004 called “Using the PDMWorks API to Automate PDM Communication, Integration, and Analysis”. That presentation is still available here. Note that the code samples are written in VB6, so unless you still have an old copy of Visual Studio 6.0 installed, you might have a tough time getting to the code.

Finally, don’t forget about the SolidWorks API forums. Presently, within the documents section, there is one WPDM API example covering triggers.

Know of any other useful WPDM API resources? Please share in the comments.

Enjoy!

Want to keep up with new CADSharp.com content? Sign up for our newsletter.


Leave Comment

Free SolidWorks API Training Webinars in June

Blog 5.16.2012 4 Comments


CADSharp.com is proud to announce its upcoming SolidWorks API training webinars for the month of June, 2012, covering a range of important API topics. Please note that these times are not set in stone, and I am willing to change times or add more times depending on level of interest.

Interested in attending a webinar? Please sign up for our newsletter to receive details.

Introduction to the SolidWorks API

Time: Friday, June 1, 5:00 PM GMT (12:00 PM EST)
Duration: 1 hour
API skill level: Beginner

Looking to finally get your hands dirty with the SolidWorks API? No experience is necessary for this hands-on introduction to the SolidWorks API. The macro recorder, API Help, basic VBA programming, and much more will be demonstrated with easy-to-follow examples. Attendees are welcome to ask questions during and after the presentation.

Automating Custom Properties

Time: Thursday, June 21, 5:00 PM GMT (12:00 PM EST)
Duration: 1 hour
API skill level: Intermediate

This presentation assumes that you have a basic understanding of the SolidWorks API, particularly how to use the API Help. You will learn how to access and use the ICustomPropertyManager interface to control custom properties at the document and configuration specific level. This will include adding, modifying, deleting, and traversing custom properties. Attendees are welcome to ask questions during and after the presentation.

Again, you will need to sign up for our newsletter to receive additional webinar details.


Leave Comment