Is it possible to write an API code
Is it possible to write an API code
(OP)
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.
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.






RE: Is it possible to write an API code
RE: Is it possible to write an API code
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.
RE: Is it possible to write an API code
RE: Is it possible to write an API code
DimensionalSolutions@Core.com
While I welcome e-mail messages, please post all thread activity in these forums for the benefit of all members.
RE: Is it possible to write an API code
DimensionalSolutions@Core.com
While I welcome e-mail messages, please post all thread activity in these forums for the benefit of all members.
RE: Is it possible to write an API code
RE: Is it possible to write an API code
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>
RE: Is it possible to write an API code
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 "Set" keyword when setting an object variable. The SW API help will not remind you of this fact.
RE: Is it possible to write an API code
RE: Is it possible to write an API code
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.
RE: Is it possible to write an API code
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 www.msn.com).
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.
Consciousness: That annoying time between naps.
RE: Is it possible to write an API code
ex:
Dim MySheeet as Sldwrks.Sheet
Set MySheet = MyDrawing.Sheets.Item(I)
' - or -
Set MySheet = MyDrawing.Sheets.Item("Sheet1")
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 "unique" name to access it.
RE: Is it possible to write an API code
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.