I need to write a macro that will get the name of the current sketch and append it to some text which will then be used as a value for a custom property. I've seen one posting on this and tried it but it doesn't work. Can anyone help?
Thanks
I need to write a macro that will get the name of the current sketch and append it to some text which will then be used as a value for a custom property. I've seen one posting on this and tried it but it doesn't work. Can anyone help?
Thanks
Since API calls can change from version to version, what version of SolidWorks are you using?
Edit: From SW2017
Dim swApp As SldWorks.SldWorks
Dim swDoc As SldWorks.ModelDoc2
Dim swSketchMgr As SldWorks.SketchManager
Dim swActiveSketch As SldWorks.Sketch
Sub main()
Set swApp = Application.SldWorks
Set swDoc = swApp.ActiveDoc
Set swSketchMgr = swDoc.SketchManager
Set swActiveSketch = swSketchMgr.ActiveSketch
MsgBox swActiveSketch.Name
End Sub
I like this code better (mainly because Feature.Name is documented whereas swSketch.Name is not {as least I could not find it}):
Dim swApp As SldWorks.SldWorks
Dim swDoc As SldWorks.ModelDoc2
Dim swActiveSketch As SldWorks.Sketch
Dim swFeat As SldWorks.Feature
Sub main()
Set swApp = Application.SldWorks
Set swDoc = swApp.ActiveDoc
Set swActiveSketch = swDoc.GetActiveSketch2
Set swFeat = swActiveSketch
MsgBox swFeat.Name
End Sub
Thanks for the response. I'm running SW2016. I've tried both versions posted and while they both work in a message box, they don't work within the program I've written. What I've done is made a custom property called OD1 and am trying to use a text box to enter the dimension name (D!,D2, etc)and appending the @ symbol with the dimension name that's entered into the text box The error I keep getting is "Object variable or with block not set", happening at the line in bold text. Code as follows:
Private Sub btnok_Click()
Dim swApp As SldWorks.SldWorks
Dim swDoc As SldWorks.ModelDoc2
Dim swSketchMgr As SldWorks.SKETCH
Dim swfeat As SldWorks.feature
Set swApp = Application.SldWorks
Set swDoc = swApp.ActiveDoc
Set swActiveSketch = swDoc.GetActiveSketch2
Set swfeat = swActiveSketch
Set swModel = swApp.ActiveDoc
Set swCustPropMgr = swModel.Extension.CustomPropertyManager("")
Set Part = swApp.ActiveDoc
swCustPropMgr.Set "SERIES", txtseries.Text
swCustPropMgr.Set "POINT", txtpoint.Text
swCustPropMgr.Set "DUCTDIA", txtductdia.Text
swCustPropMgr.Set "DUCTBC", txtductbc.Text
swCustPropMgr.Set "OD1", txtod1.Text + "@" + swfeat.Name
Set swApp = Nothing
Unload Me
End
End Sub
Thanks in advance.
Try these codes. I've cleaned them up and removed extra lines.
Also replaced Set with Add3. This would make sure that if a property does not exist then it would add the property and value.
Private Sub btnok_Click() Dim swApp As SldWorks.SldWorks Dim swDoc As SldWorks.ModelDoc2 Dim swfeat As SldWorks.Feature Dim swCustPropMgr As SldWorks.CustomPropertyManager Dim swActiveSketch As SldWorks.Sketch Set swApp = Application.SldWorks Set swDoc = swApp.ActiveDoc Set swCustPropMgr = swDoc.Extension.CustomPropertyManager("") Set swActiveSketch = swDoc.GetActiveSketch2 Set swfeat = swActiveSketch swCustPropMgr.Add3 "SERIES", 30, txtseries.Text, 1 swCustPropMgr.Add3 "POINT", 30, txtpoint.Text, 1 swCustPropMgr.Add3 "DUCTDIA", 30, txtductdia.Text, 1 swCustPropMgr.Add3 "DUCTBC", 30, txtductbc.Text, 1 swCustPropMgr.Add3 "OD1", 30, txtod1.Text + "@" + swfeat.Name, 1 Set swApp = Nothing Unload Me End End Sub
Good morning
Unfortunately now it's giving an error at line 10 saying object required where it was not doing that before.I am also needing to add the part name as well so the property value shows the actual dimension value if you could add that for me. I'm new to the SW API so it's been slow going. Any help is greatly appreciated.
Thank you
Frank Sandoval wrote:
I am also needing to add the part name as well so the property value shows the actual dimension value
Could you post and example of what you need. Little unclear with this line.
In the example above, the text expression at the bottom is what I'd like to be able to insert so the evaluated value on the right reflects the dimensional value. I modified line 10 per your instruction and it got past that and stopped again at line 18 with error 91 object variable or with block variable not set.
Frank
I see no issues with that line, so probably something on your end. To debug, please share the file and the macro (swp file).
Foe getting the file name you can use
swDoc.GetTitle (this may or may not give you the file extension. so you can add that as string).
OR
swDoc.GetTPathName (this give you full path if file has been saved already and you can extract the file name with extension from that path).
I've attached the sketch which is just two lines and two dimensions.named D1 and D2 along with the swp file for you to review.
Thanks
Frank
I got the text inserted the way it needs to be and have one more question if I could. How do I enclose the string of text in quotes? When I try to do this, it comes back saying that the @ symbol which is enclosed in quotes is an invalid character.
Thanks
I just figured it out. It was simply adding chr(34) at the beginning and end of the string instead of using actual quotes. Thank you for all your help, it's greatly appreciated.
I was about to add the chr(34) code but then was wondering why quotes are not working. So was rather interested to see why.
Anyway glad you got it working. You may want to give the credits (correct answer) to Solid Air post as he has the main codes. I only added to that.
You need to be in sketch edit mode before pushing the button else you would get error on that line.