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
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
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.
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?
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)
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
Lindsay,
As Tony has said look at using Design Tables which control your part using an Excel Table.
Mark
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.
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.
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.
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
SW Books:
SolidWorks 2008 API: Programming and Automation
SolidWorks 2009 API: Advanced Product Development
Plug!
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.
Good luck.
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
Lindsay,
Did you see my previous message? Are you not interested in using design tables?
-Chris
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.
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)....
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
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 20092008 Help
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"
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
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.
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.
Here a Excel File that I created for this post
Chris
I hope you are a comedian because this is funny.Dan