Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

set part (visual basic - solidworks)

Status
Not open for further replies.

sisau

Mechanical
Mar 5, 2010
19
0
0
SE
I am trying to edit a SW part using Microsoft Visual Basic. I just want to edit some dimensions. The code I am using is the following:


Set swApp = CreateObject("SldWorks.Application")

Set Part = swApp.ActiveDoc
Set myDimension = Part.Parameter("dimension2@Sketch2")
myDimension.SystemValue = Excel.Range("C4") * 0.001
Set myDimension = Part.Parameter("dimension1@Sketch2")
myDimension.SystemValue = Excel.Range("C3") * 0.001
Set myDimension = Part.Parameter("dimension3@Sketch3")
myDimension.SystemValue = Excel.Range("C5") * 0.001
Part.ClearSelection2 True
boolstatus = Part.ForceRebuild3(True)


The problem I have is using "Set Part = swApp.ActiveDoc" because I can only edit an active part. This gives me 2 main problems:
- I can't edit a part that is not active/opened.
- If I open more than one part with the same name of the dimensions, all the parts are changed. (I know I could solve this just changing the names of the dimensions, but I don't want to).

What I would like to do is put some kind of reference to "set part" in order to change the part I want and not an active part.

I hope I have explained in a good way my problem... Thank you for your help!
 
Replies continue below

Recommended for you

In the API Help, look for "accessors" under ModelDoc. You can access a model by opening a file, gettiing a component, or from a list of open files, among other ways.

[bat]Honesty may be the best policy, but insanity is a better defense.[bat]
-SolidWorks API VB programming help
 
Thank you but I didn't find how to do it. I am a beginner using VB and I have no idea about how to use it. All I have done is copy/paste from internet, so if you could correct my code I would be really grateful.
 
I think you need to set up a modeldoc2 object first then set the part object equal to that. You would do this is you want to work with a drawingdoc too, it doesn't make sense to me but that is the way they show it in the help.

Like this (I usually do things the long way but you should get the idea):

Dim theModel as sldWorks.ModelDoc2

Set swApp = CreateObject("SldWorks.Application")

Set theModel=swApp.ActiveDoc

'' Now just set the Part object equal to theModel object
set Part=theModel

HTH,
Dan
 
thank you for your help. finally i solved it using the following code:

Set swApp = CreateObject("SldWorks.Application")

Set Part = swApp.ActiveDoc

Part.Parameter("dimension1@Sketch1@Actuator1.1.Part").SystemValue = Excel.Range("C5") * 0.001

Part.Parameter("dimension2@Sketch1@Actuator1.1.Part").SystemValue = Excel.Range("C6") * 0.001

boolstatus = Part.ForceRebuild3(True)
 
Status
Not open for further replies.
Back
Top