Gemini for macro code?
Posted: Tue Jan 28, 2025 1:56 am
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.
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.