understanding the language in Visual Basic from a Solidworks macro

Hi,

I am new to Solidworks and I am trying to figure out how to change the codes in Visual Basic; say for example; the dimensions of a part.  thanks for your time

SolidworksApi macros
Answers 32
SF
SF 2009-10-06 Best Answer

If you would like to learn the language from the beginning you probably should purchase some books, cources. I believe there should be some on-line trainings for Visual Basic. But it will take some time.

If you need some brief skills for example to modify the recordered macro I would like to propose the following:

Once the macro recordered analize its code and refering to API Help Documentation identify which method for. Use F8 key to debug the macro step-by-step and you can see which line of code actually doing.

Macro usually writes some excess code (for example view rotation, selection etc) which can be removed from the macro. The step-by-step debuging would help to identify such lines.

Please do not hesitat to ask questions.

SF
SF 2009-10-06

Thanks for the quick reply.  It definitely is a massive topic to understand and would take time to learn properly.  But as I do not have that luxuary at the minute I am just trying to go for the basics.

I used the F8 command like you said (very useful indeed to see whats going on) and stripped out all the unnecessary code. Is there any quick way of say editing the coordinates so that if I knew the height of the line/part that I wanted it to be, I could enter it in somewhere in the code?

MG
MG 2009-10-06

Hi Lindsay

This might sound stuipid but why would you want to use a macro to control a part size.

Why not use the parmeters that control a part, from the base sketch.

Thanks

Mark (Solidworks 2009 SP4.1) 

SF
SF 2009-10-06
I eventually want to link the code into Excel where I can just; say for example; enter in the dimensions of the part into a easily set-up spreadsheet and Solidworks will draw up the part.
SF
SF 2009-10-06
There is only one method in SOlidWorks for drawing the line: ModelDoc2::CreateLine2 which expects the coordinates of two end points. However you can create your custom function which will create a line by for example a start point and height.

But note that the start point and the length is not enought to define the line. The angle should be defined as well.

The sample of such function. (Note. The end point's coordinates should be calculated):

Sub CreateLineByLength (startX as Double, startY as Double, length as Double, angle as Double)
   ...
   Part.CreateLine2 startX, startY, 0, <...>, <...>, <...>
   ...
End Sub
SF
SF 2009-10-06
Have thought about design tables?
SF
SF 2009-10-06
design tables? what do you mean?
MG
MG 2009-10-06

Lindsay,

As Tony has said look at using Design Tables which control your part using an Excel Table.

Mark

SF
SF 2009-10-06
Ok
SF
SF 2009-10-06

Not knowing how to program this is going to be a very hard thing to do. You can input all your values into a excel file and then import that. Look for design tables in the help, there are some great examples on the Internet so go do a search. if you have never worked with these it will be a learning curve but a lot shorter learning curve the trying to use VB to do this.

JB
JB 2009-10-06

If you've never done any Visual Basic programming, it will take you a full week to accomplish this task working on it 8 hours a day.

Or, you can set up a basic part with a design table, and accomplish this in 3-4 hours.

Take your pick.

SF
SF 2009-10-06
what form does the data have to be in for Solidworks to use those parameters?
PM
PM 2009-10-07

Lindsay,

  look through the help file for design tables.  you should create your part and then create a configuration and modify any parameters that change for the configurations then use the auto-create option.  This will give you an excel table that you can use to create all your part variations.  Seriously review the help to get you going.

MG
MG 2009-10-07

Hi Lindsay,

Attached is a couple of screen shots of a very basic shape driven by a design table.

Create you shape & dimension / extrude to thickness.

Go to Insert ---- Table ---- Design Table. Use Auto create. Select dimensions you want to control.

An Excel table will appear with sketch / extrude dimensions (or the ones you selected).

Alter a dimension and click outside table (this will close table) the part will update to new dimension.

To see table again go to Configurations in Design tree, right click Design Table, edit table.

Hope this helps.

In the screen shots i altered D1 @ sketch1 from 100mm to 250mm. After closing Excel table, part updated to new length.

Thanks

CF
CF 2009-10-08

Lindsay,

  Take a look at the attached part.  Click on the Configuration Manager tab at the top of the feature tree.  In there, you should see a Design Table object.

If you right-click on this, you should be able to Edit Table in New Window.  This will open an excel spreadsheet with all the dimensions of this part in the default configuration and the Config2 configuration.  You can change the values in the spreadsheet, and the dimensions will change in the corresponding configurations.  You can even copy a row and rename the first column of that row and create a new configuration with different dimensions.  When you close the spreadsheet, SolidWorks will ask if you want to create new configurations for any rows you may have added.

  This is a pretty cool feature.

  • If you have your own part and you want to do something similar, it will be easiest if you create at least 2 configurations and change the relevant dimensions in each configuration.  When you change the dimensions notice that on the Modify form, there are a few icons under the dimension.  The last one on the right allows you to specify where you want to change the dimension.  If you don't select THIS CONFIGURATION, that dimension may change in all other configurations.
  • You should set a different value for each dimension that you want to vary (through the spreadsheet) in each configuration.  For example, if you want to vary the Width, make sure you set the width to a different value in each configuration, etc.
  • Now you should be able to automatically create a design table.  If you don't see the TABLE toolbar anywhere on your screen, you can right click on a blank area of your toolbar and check TABLE.  You will see a new toolbar show up.  If you click the second icon from the end of this toolbar, you will get the Design table dialog.  You can just choose autocreate to get used to the idea.

Good luck.

JC
JC 2009-10-12

I Learned all my SolidWorks Programing with just the API Help

in Solidworks select Help in the then API Help

I know how to export to Excel and i should be able to inport info to solidworks

let me know if you still need the code

SF
SF 2009-10-12
How do you export to excel?
CF
CF 2009-10-12

Lindsay,

Did you see my previous message?  Are you not interested in using design tables?

-Chris

JB
JB 2009-10-12

Here's a parable:

Noob:

"I've never been on the road before, but here I am on the sidewalk and now I want to have a car.  Can someone tell me how to build one?"

Car manufacturer:

"Why don't you go down to a car dealer and buy one?  It will cost less and work better."

Noob:

"What is a car dealer?"

Driving enthusiast:

"Look up 'Car Dealer' in the yellow pages and call one."

Other driving enthusiast:

"Hey, check out the car I bought from a dealer last week.  It works great! Take it for a test drive."

Car manufacturer:

"It takes a long time to learn how to build cars.  If you've never driven before, building a car will be nearly impossible to do. You should really look into buying one."

Noob to Car Manufacturer:

"How do you make a car?  I'm at an auto parts store and there's just so many parts!"

Driving Enthusiast:

"Did you try out the car I sent you?"

Methinks a course in Driver's Ed might be appropriate.

CF
CF 2009-10-12

Whatever you decide to do, don't try DESIGN TABLES!!!!

They are really hard to use and require abundant brain power  .  Only the uber-intelligent should try them after first going through the easy method of writing a visual basic or C# application to do it manually (which, of course, is much easier)....

PM
PM 2009-10-12
just type over the dimension value with what you want... you know... like in AutoCAD
JC
JC 2009-10-12

here is the macro

I use Excel 2008 in the marco if you have a different  ver. click Tools-References

then find microsoft Excel X Object Library and check the check box then click k

you need Excel installed on the computer

there are two sub Export and a import

--------------------------------------------------------
Dim swApp As Object
Dim excelapp As Excel.Application
Dim WorkBook As Excel.WorkBook
Dim SWExport As Excel.worksheet


Sub exporttoExcel()
Set swApp = Application.SldWorks
Set excelapp = New Excel.Application
excelapp.Visible = True
Set WorkBook = excelapp.Workbooks.Add()
Set SWExport = WorkBook.ActiveSheet

SWExport.Name = "SWExport"
'' write to A2
excelapp.range("A2").Select
excelapp.ActiveCell.FormulaR1C1 = "Write to a Cell"
End Sub


Sub ReadfromExcel()
Set swApp = Application.SldWorks
Set excelapp = New Excel.Application
excelapp.Visible = False
FileName = "C:\temp\importinfo.xls"
Set WorkBook = excelapp.Workbooks.Open(FileName)
'' read A2
excelapp.range("A2").Select
MsgBox (excelapp.ActiveCell.FormulaR1C1)
End Sub

Let me know if you need more help

comment imagecomment image
DM
DM 2009-10-12

I do not find the 2009 API help very helpful if I do not understand the objects first, although it is nicer for looking up the methods. If a person has access to SW 2008 the help is friendlier especially for a person who has not worked with VB. In 2008 it is easier to find examples.To work with Excel you need to know how to work with Excel objects in addition to SolidWorks plus normal Visual Basic.

Dan Miel

SW 2009

2008 Help

JB
JB 2009-10-12

FYI, that method of reading/writing cells is pretty inefficient.  Selecting cells in Excel will waste resources.  Also, it is much easier to loop through Excel cells by number rather than by combination of letters and numbers.

'' write to A2
excelapp.range("A2").Select
excelapp.ActiveCell.FormulaR1C1 = "Write to a Cell"

is the same as

'' write to A2
excelapp.ActiveWorkbook.Cells(2, 1).FormulaR1C1 = "Write to a Cell"

JC
JC 2009-10-12

Thank you for the update

Sub exporttoExcel()
Set swApp = Application.SldWorks
Set excelapp = New Excel.Application
excelapp.Visible = True
Set WorkBook = excelapp.Workbooks.Add()
Set SWExport = WorkBook.ActiveSheet

SWExport.Name = "SWExport"
'' write to A2
SWExport.Cells(2, 1).FormulaR1C1 = "Write to a Cell"
End Sub

Sub ReadfromExcel()
Set swApp = Application.SldWorks
Set excelapp = New Excel.Application
excelapp.Visible = False
FileName = "C:\temp\importinfo.xls"
Set WorkBook = excelapp.Workbooks.Open(FileName)
Set SWExport = WorkBook.ActiveSheet
'' read A2
MsgBox (SWExport.Cells(2, 1).FormulaR1C1)
End Sub

DS
DS 2009-10-13
I'd like to pile some hate on the 2009 API help, as well.  I'm honestly not sure if I'd have been able to teach myself how to do the programming I do with the current help files.
RS
RS 2009-10-13

Have you looked into design tables?  Sounds like what you want can be accomplished in a DT without need for macros.

Recorded macros are a mediocre beginning to understanding VB and SW API.  Recorded macros tend to miss many important commands.  The results are often unusable, especially when entity selection is involved.  The convoluted method of recording can lead to a poorer understanding of how SW API is actually structured.

SF
SF 2009-10-13

Create you shape & dimension / extrude to thickness.

Go to Insert ---- Table ---- Design Table. Use Auto create. Select dimensions you want to control.

>>> Somehow the menu tab "Design Table" is grayed out.  Did I do something wrong?

Thanks for your instruction.

SF
SF 2009-10-13
I saved a copy of my 2008 API help file and use it most of the time. Only use 2009 to see if something new was added.
JC
JC 2009-10-14

Here a Excel File that I created for this post

DM
DM 2009-10-15

Chris

I hope you are a comedian because this is funny.

Dan