Page 1 of 1

Gemini for macro code?

Posted: Tue Jan 28, 2025 1:56 am
by Petertha
I'm a novice Gemini user, even less familiar with ChatGPT. When I was learning VBA for Excel I found Gemini beneficial to check code or ask questions or even suggest code. Sometimes it was surprisingly good, other times it required more work or a lengthy dissection conversation to figure out what it was doing (or not doing). Does anyone here use it or have comments in this regard?

Maybe a bad first example I was exposed to a very nice macro on another thread that saved all bodies out as .STL files. It worked. I typed in this request into Gemini & well, it didn't. Now maybe its using some syntax a more experienced user would figure out that is above my VBA pay grade right now. But anyway I'll paste just for discussion purposes. Like whats with the oDoc, oComp ('o' prefix)

can you write solidworks vba code to save all solid bodies as seperate .STL files

Sub SaveSolidBodiesAsSTL()

Dim oDoc As Document
Dim oComp As Component2
Dim oBodies As Bodies2
Dim oBody As Body2
Dim oFeatures As Features
Dim oFeature As Feature
Dim i As Integer
Dim strFileName As String

' Get the active document
Set oDoc = ThisApplication.ActiveDoc

' Check if the active document is a part document
If oDoc.Type <> swDocPART Then
MsgBox "This macro is only for part documents.", vbCritical
Exit Sub
End If

' Get the root component
Set oComp = oDoc.GetComponent(0)

' Get all bodies in the root component
Set oBodies = oComp.GetBodies2

' Loop through each body
For i = 1 To oBodies.Count
' Get the current body
Set oBody = oBodies.Item(i)

' Create a new feature to suppress all other bodies
Set oFeatures = oComp.Features
Set oFeature = oFeatures.AddSuppress(swSuppressTypeBody, oBodies)

' Suppress all other bodies except the current one
For j = 1 To oBodies.Count
If j <> i Then
oFeature.SetSuppressed(j - 1, True)
End If
Next j

' Create the file name
strFileName = "Body_" & i & ".stl"

' Save the part as STL
oDoc.SaveAs strFileName, swSaveAs2, 0, 0, 0, False

' Unsuppress all bodies
oFeature.SetSuppressed(0, False)

' Delete the temporary suppression feature
oFeatures.Remove oFeature

Next i

MsgBox "All solid bodies saved as separate STL files.", vbInformation

End Sub
Explanation:

Get Active Document:

Retrieves the currently active document object.
Checks if it's a part document; otherwise, displays an error message.
Get Root Component:

Obtains the root component of the part.
Get All Bodies:

Retrieves a collection of all bodies within the root component.
Loop Through Bodies:

Iterates through each body in the collection.
Create Suppression Feature:

Creates a new suppression feature to temporarily hide all other bodies.
Suppress Other Bodies:

Within the suppression feature, suppresses all bodies except the current one.
Create File Name:

Constructs a file name for the STL file using the body index.
Save as STL:

Saves the part as an STL file with the generated file name.
Unsuppress All Bodies:

Re-enables all suppressed bodies.
Delete Suppression Feature:

Removes the temporary suppression feature from the part.
Display Message:
Shows a success message indicating that all bodies have been saved.
How to Use:

Open the desired part file in SOLIDWORKS.
Press Alt + F11 to open the VBA editor.
Insert a new module (Insert > Module).
Paste the code into the module.
Run the macro by pressing F5 or clicking the Run button.
This code will create separate STL files for each solid body in your part, making it easier to work with individual components or perform downstream processes like 3D printing.

Re: Gemini for macro code?

Posted: Tue Jan 28, 2025 3:43 am
by Stefan Sterk
Hi Petertha,

First thing, this topic has a better fit under the API section rather than the Macro Library.

I've tried using ChatGPT, but I often had to make adjustments. I wouldn't recommend it for beginners, as it can sometimes lead you in the wrong direction.

As for the 'o' in oDoc, I believe it stands for "Object." You typically see swDoc used in example codes since it's a SOLIDWORKS object. Naming your variables this way is user-friendly, as it helps you easily identify what you're working with.

Re: Gemini for macro code?

Posted: Wed Jan 29, 2025 5:19 pm
by Hansjoerg
I have also tried to write macros for Solidworks with Chat GBT, for very simple tasks it works quite well, but for complex tasks the generated code was often faulty.
That's why I stopped creating code with AI. It's OK to get some inspiration or to get a hint about the required interfaces. But I write the final code myself, because it is important to me to understand what I am programming and how the code works, or why it doesn't work :-)

If you want to learn how to write macros in SWX without AI, have a look at this article: viewtopic.php?t=3811

Re: Gemini for macro code?

Posted: Thu Jan 30, 2025 1:14 am
by Monstrum Mathias
Hi Petertha

The attached "Save parasolid.swp" macro saves all or selected bodies as separate parasolid files. I made a variation for exporting STEP-files very easily. Just look for the file extension within the code and change it to STL.

Re: Gemini for macro code?

Posted: Thu Jan 30, 2025 7:40 am
by DLZ_SWX_User
Thank you @Monstrum Mathias ! I have tried several different macro's to save as STEP or parasolid but haven't found one that did want I wanted. These do as close as what I wanted as I have found anywhere! Thanks again.