×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

set part (visual basic - solidworks)

set part (visual basic - solidworks)

set part (visual basic - solidworks)

(OP)
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!

RE: set part (visual basic - solidworks)

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.

batHonesty may be the best policy, but insanity is a better defense.bat
http://www.EsoxRepublic.com-SolidWorks API VB programming help

RE: set part (visual basic - solidworks)

(OP)
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.

RE: set part (visual basic - solidworks)

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
 

RE: set part (visual basic - solidworks)

(OP)
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)

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources