Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

Is it possible to write an API code

Status
Not open for further replies.

rsands

Mechanical
Sep 25, 2002
6
Is it possible to write an API code that will open a specific Solidworks file and edit it based on your values. In other words, instead of writing a code with
Set Part = swApp.ActiveDoc so that you can tell Excel to edit the active document, is there a way to be more specific and edit a certain part? Any help would be appreciated. Thanks.
 
Replies continue below

Recommended for you

You can use OpenDoc6. You have to know what the pathname for the document is. If the drawing is already loaded you can use GetOpenDocumentByName.
 
You may want to use the "silent" option when opening the part.

I have had poor luck trying to edit parts that are not the active document. Sometimes I get an error that the program has become disconnected from the file object.

If I want an program to alter "File B" when "File A" is the active document, I keep the object for "File A", make "File B" active, and return to "File A" when finished. If necessary I also mark whether "File B" was visible or loaded before the program started.

[bat]I may make you feel, but I can't make you think.[bat]
 
Thank you for your responses. However, because I know very little of VB, I have know idea how to do any of these things. What I have so far is what I have found on the net. I also forgot to mention that once I open the specific file that I want to be able to change part dimensions that I have called out in Excel.
 
Where do you want to write the code (i.e. VB, Excel or SolidWorks)? You will have to have some knowledge of the Excel and SolidWorks APIs to accomplish what you want.

DimensionalSolutions@Core.com
While I welcome e-mail messages, please post all thread activity in these forums for the benefit of all members.
 
Have you considered using a design table? You can drive your dimensions with Excel directly in SolidWorks.

DimensionalSolutions@Core.com
While I welcome e-mail messages, please post all thread activity in these forums for the benefit of all members.
 
I want to write the code within Excel. Are there any books that might help me in this area?
 
<piss-and-moan-mode ON>

You'd think, tho, that once you have this OBJECT (be it document, sheet, or view) that you'd just need to reference it by its object handle, and not have to re-reference it by its damn title.

If you've programmed multiple workbooks/multiple worksheets in excel API, you'd know what I mean.

I feel as though I should keep a box of RITZ around, for when the cheese shows up.

<piss-and-moan-mode OFF>
 
I picked up VBA for Dummies and Visual Basic 6 Weekend Crash Course from Barnes and Noble when I first got going on VB. They taught me most of what I need to know as far as object oriented programming goes.

If you're and old-school FORTRAN/BASIC programmer like me, it takes a little attitude adjustment. To begin with, there are many more types of variables to remember, and the programming approach is not linear. Once you get the hang of properties vs. methods vs. accessors, and functions vs. subs, you've cleared the biggest hurdle.

Next, pay attention to the different types of SW objects that are available in the API. A component is not a model is not necessarily a part. Also, pay close attention to the return values for some of the methods. Make sure you are using the appropriate type (variant, boolean, object, etc.). Always use the &quot;Set&quot; keyword when setting an object variable. The SW API help will not remind you of this fact.

[bat]I may make you feel, but I can't make you think.[bat]
 
I second the &quot;VBA for Dummies&quot;, it was used as the text for a SW sponsored API training class! I also recommend MS VB 6.0 Deluxe Learning Edition for anyone who might not have access to the real thing.
 
rocheey,

What exactly is the problem you're having? I've done a lot of programming with Excel and I've never had a problem with object handles not giving me access to the object.

rsands,

The SolidWorks API is very poorly documented. I would recommend sticking with Excel for a while, as it has far better documentation. It's hard enough to learn VBA as it is, without making life harder for yourself by trying to learn 2 APIs at the same time.
 
rosands
030203usf_prv.gif


There are a couple of problems that need to be resolved when you write Macros in Excel. Microsoft – in their self-proclaimed wisdom (is it an exalted state or is it just obnoxious) – decided that every macro is a potential virus threat. So they instituted a Certification process that does work fairly well. It allows Trusted companies to issue Digital Certificates to other Trusted companies for a price. A Spreadsheet (or any MS file) that contains an embedded Macro that has a Digital Certificate runs without question (well – sort of). Without a Certificate – by default – all macros are ignored – even the ones that you create on your system for your own use.

This can be overcome by changing the Security Level in Excel from High to Medium - which prompts you when the file is opened. Selecting Tools\Macro\Security does this – The warning message is VERY UGLY and is almost guaranteed to make most people disable the Macro. I use this setting primarily during a Macro’s testing stage.

SolidWorks chose to follow Microsoft’s lead. If there is a Design Table in a Model or a Drawing that contains a Macro without a certificate – and the Security Level is High – the Macro is ignored. If the Security Level is Medium – then you get SolidWorks version of a VERY UGLY message (it still has the same guarantee though).

A Digital Certificate is needed to use a finished Macro so that you do not see this message. Microsoft included a program to do this that avoids the cost of getting certified by another company – but the drawback is that every machine that will run the macro needs to run this program first and have a Certificate with the same name. – Start Windows Explorer and look in your Program Files\Microsoft Office\Office directory for the program named SelfCert.Exe - This program is not always installed by Office so if it doesn’t exist you can find it on your installation CD (or you can download it from
Each time you run SelfCert.Exe – it will create a Digital Certificate on your system with the name you give it. – If other people will be using these files and your macros – They need to run the program on their system as well - I would suggest that you use your company name or the company’s initials for the Certification’s Name rather than your own name.

Once this is accomplished – Start Excel and save the new spreadsheet to someplace where you can find it again (like your desktop) - Do a Tools\Macro\Visual Basic Editor – In the VBA editor do a Tools\Digital Signatures and select the Choose button – Your new certificate will be displayed – Select it and hit OK twice – Hit Save in the VBA editor and close it

If you changed your Security Level – you should reset it to High – Tools\Macro\Security – and close Excel.

Now – any time that you need to create a macro in a spreadsheet you can use this file as a template. If you choose to start a new file – you will need to select the Digital Signature within the macro.

Random_Shapes_Pointed_shapes_prv.gif
Lee
Random_Shapes_Pointed_shapes_prv.gif


Consciousness: That annoying time between naps.
 
bobsheets : Im not having any problems in that regard. I just wish one could access a Sheet/Sheets class, doc/docs class in SW, the way one can in excel, etc.

ex:
Dim MySheeet as Sldwrks.Sheet
Set MySheet = MyDrawing.Sheets.Item(I)
' - or -
Set MySheet = MyDrawing.Sheets.Item(&quot;Sheet1&quot;)

you get my drift....

it just seems silly to have to use a string variable to access an an object handle that you already have. SW API will let you name every sheet with the SAME NAME, but then you need to pass the sheets &quot;unique&quot; name to access it.
 
This pasted from SW API help

Ensure that Visual Basic code is early bound by adding the SolidWorks type library, SldWorks.tlb, to the project references and declaring variables as their real type, for example, Feature or Body2 instead of Object.

Early binding increases type checking and assists in detecting errors in your code.


[bat]I may make you feel, but I can't make you think.[bat]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor