Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading Custom Properties from a Part 1

Status
Not open for further replies.

VELCROW

Mechanical
Apr 30, 2008
92
I wrote a macro to automate filling in custom properties. For example, to make the custom property DESCRIPTION1="Widget"

Set swApp = Application.SldWorks
Set swDoc = swApp.ActiveDoc
swDoc.CustomInfo2("", "DESCRIPTION1") = "Widget"

Now I want to read in an existing description in a part, and if it exists, reuse it. How can I read it in?

(Sorry if it is obvious, I couldn't find the answer)

Thanks,

Steve
 
Replies continue below

Recommended for you

You may not need a macro (nor want to use one) for this if you are working with trying to automate drawing creation. SolidWorks has a built in function to link drawing annotations to custom properties of the part, and it auto-updates those when the part changes.

Matt Lorono
CAD Engineer/ECN Analyst
Silicon Valley, CA
Lorono's SolidWorks Resources
Co-moderator of Solidworks Yahoo! Group
and Mechnical.Engineering Yahoo! Group
 
I have a custom macro to put in custom props, and I am modifiying existing parts. If the description is still valid, I don't want to retype it. So I just want to read the description in.
 
Steve,

Try this:

Dim swApp as SldWorks.SldWorks
Dim swModeal as SldWorks.ModelDoc2
Dim swCusPropMgr as SldWorks.CustomPropertyManager
Dim AddStatus as Long
Dim Value as String
Dim ResValue as String

'add/create custom property
AddStatus = swCusPropMgr.Add2("Description", swCustomInfoText, "Widget")

'get custom property and...
swCusPropMgr.Get2 "Description", Value, ResValue

'display it
Msgbox "Value for Description is: " & Value

Hope this helps.

Best regards,


Chris Gervais
Application Engineer
CSWP, CSWST
 
Chris,

I pasted that into a new sub, and got a VB error 91, object variable or With block variable not set on this line

AddStatus = swCusPropMgr.Add2("Description", swCustomInfoText, "Widget")

Any ideas?

Thanks,

Steve
 
I'd say make a UserForm with a TextBox and an OK button. Link the TextBox to the Description, and if it's there, it will fill it in. If it's not there, it will be blank. That way you can change it if needed, or leave it how it is if it's correct.

I've never used the CustomPropertyManager, so this method may be a bit outdated, but it works.

Here are some bits and pieces from something I have already made:

Private Sub UserForm_Initialize()
TextBox1.Text = swDoc.GetCustomInfoValue("", "DESCRIPTION")

Private Sub CommandButton1_Click()
swDoc.DeleteCustomInfo2 "", "DESCRIPTION"
swDoc.AddCustomInfo3 "", "DESCRIPTION", 30, TextBox1.Text

If you don't want to use the UserForm, you can condense it into an If/Then statement. I just like the UserForm because it shows you the description as is, and you have the option to leave it alone, modify it, or add it if it's blank.

 
I assume you are talking about copying a property to a drawing from the part or assembly in the drawing.

The path to get to the part is a bit convoluted, but you can get there. From the drawing, you need to determine which view is the view from which properties are taken. Once you have the view, you connect to the part from there.

[bat]Honesty may be the best policy, but insanity is a better defense.[bat]
-SolidWorks API VB programming help
 
gopack,

That is what I needed. I just did

DESC = swDoc.GetCustomInfoValue("", "DESCRIPTION")

and I got what I need.

It's a very straightforward command, but in all my looking I never found it.

Thanks,

Steve
 
I would amend gopack's script just a little. There is no need to delete the custom info.

First, change the property using swDoc.CustomInfo2, then swDoc.AddCustomInfo3 "", "DESCRIPTION", 30, TextBox1.Text.

Instead of blindly deleting the property, this would simply change the property if it is already there and then add it if is not already there.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor