Gemini for macro code?
Gemini for macro code?
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.
- Stefan Sterk
- Posts: 45
- Joined: Tue Aug 10, 2021 2:40 am
- x 67
- x 85
Re: Gemini for macro code?
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.
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?
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
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
All the "good" news about SWX makes me feel like I'm driving a truck with two trailers straight into a dead end.
-
- Posts: 15
- Joined: Mon Apr 15, 2024 2:03 am
- x 7
- x 13
Re: Gemini for macro code?
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.
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.
- Attachments
-
Save STEP.swp
- (41.5 KiB) Downloaded 27 times
-
Save parasolid.swp
- (43 KiB) Downloaded 29 times
-
- Posts: 79
- Joined: Mon Dec 20, 2021 1:40 pm
- Location: Thumb Area of Michigan, USA
- x 274
- x 41
Re: Gemini for macro code?
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.
Re: Gemini for macro code?
I don’t want to sound like a party pooper, but I would strongly discourage people from using GPTs. Here are a few reasons:
(This post was proofread by a GPT.)
- The public data used to train GPTs is limited and often unreliable.
- GPTs can enter a hallucinatory state, generating nonexistent API calls.
- Most novice users end up spending more time fixing the GPT-generated code than writing it themselves.
(This post was proofread by a GPT.)
Re: Gemini for macro code?
Yeah, pretty much all the AI generated stuff I've seen for SolidWorks is so bad that you can't really even debug it unless you already know how it should have been written. If you already know how to write the macro, AI-generated code might save you some typing time getting to a starting point, but unless you can instantly recognize the non-existent API calls that it hallucinates, you will be chasing your tail for quite some time.
- Frederick_Law
- Posts: 1994
- Joined: Mon Mar 08, 2021 1:09 pm
- Location: Toronto
- x 1669
- x 1515
Re: Gemini for macro code?
AI is as good as the data used to train it.
How much SW API code was used to train it? Probably not enough.
How much SW API code was used to train it? Probably not enough.
Re: Gemini for macro code?
I find AI super useful for exploring other ways of solving problems. It also helps me confirm when API functions I am looking for don't exist becauae AI will hallucinate them. If I had colleagues to work with, perhaps I wouldn't use AI, but for now, I only have AI and thus forum.
- Frederick_Law
- Posts: 1994
- Joined: Mon Mar 08, 2021 1:09 pm
- Location: Toronto
- x 1669
- x 1515
Re: Gemini for macro code?
It's super interesting, the idea that AI can contaminate the very models that it depends on to provide useful responses. Fortunately,, I don't think we have too many concerns about this in the context at hand.
- AlexLachance
- Posts: 2322
- Joined: Thu Mar 11, 2021 8:14 am
- Location: Quebec
- x 2521
- x 2134
Re: Gemini for macro code?
See it as someone training a new employee. If the person is bad at communicating or any of the task affiliated with the training of the new employee, the new employee is set-up to fail unless someone corrects something or the employee is extremely ressourceful.
I find LLM to be fascinating for that aspect and find it to be excruciatingly dishonest when presented as a solution to every miracle the human as encountered. LLM's are only as reliable as their counterpart, and while humans are reliable, their judgement isn't always is and so a LLM becomes contaminated by prejudice imposed upon it.
For instance, so many people tout LLM as the new AI to create your drawings. I know I'm not perfect and the same thing can be said for my collegues, so if the LLM bases itself off our drawings, it will eventually repeat the mistakes we do until they are adressed in the LLM, and at that moment who knows how much damage has been done since they were introduced.
- Frederick_Law
- Posts: 1994
- Joined: Mon Mar 08, 2021 1:09 pm
- Location: Toronto
- x 1669
- x 1515
Re: Gemini for macro code?
LLM works when training data is limited, ie statistic research with controlled data.
Once random data is mixed in, you can't tell horse $hit form bull $hit.
I don't know why they let AI pulling data from internet.
Most won't let their kids learn everything from the internet.
Oh, what's the new AI called?
DeepS.....
Once random data is mixed in, you can't tell horse $hit form bull $hit.
I don't know why they let AI pulling data from internet.
Most won't let their kids learn everything from the internet.
Oh, what's the new AI called?
DeepS.....
Re: Gemini for macro code?
It'has been decades since I studied AI academically, but there is an important aspect to keep in mind. Along with the result that we get, there is an accompanying CONFIDENCE FACTOR. Without the CF, the result is less useful. Unfortunately, with the AI tools I have seen, we don't get to see the CF.
My point is that from the get go, we expected AI to give us results that are not 100% true and have been trying to quantify the statistical trueness of the responses. Its a tool and we need to learn how to utilize it.
My point is that from the get go, we expected AI to give us results that are not 100% true and have been trying to quantify the statistical trueness of the responses. Its a tool and we need to learn how to utilize it.
- Frederick_Law
- Posts: 1994
- Joined: Mon Mar 08, 2021 1:09 pm
- Location: Toronto
- x 1669
- x 1515
Re: Gemini for macro code?
Can we please leave politics out of our forum?
- Frederick_Law
- Posts: 1994
- Joined: Mon Mar 08, 2021 1:09 pm
- Location: Toronto
- x 1669
- x 1515
Re: Gemini for macro code?
Sorry, that wasn't my intention.
Re: Gemini for macro code?
Sorry about that. If an admin wants to transport it to a more appropriate section, please do! And thanks for the replies in any event.Stefan Sterk wrote: ↑Tue Jan 28, 2025 3:43 am Hi Petertha,
First thing, this topic has a better fit under the API section rather than the Macro Library.