Gemini for macro code?

Library for macros
Petertha
Posts: 62
Joined: Sun May 26, 2024 3:34 am
Answers: 0
x 14

Gemini for macro code?

Unread post 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.
User avatar
Stefan Sterk
Posts: 45
Joined: Tue Aug 10, 2021 2:40 am
Answers: 4
x 67
x 85

Re: Gemini for macro code?

Unread post 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.
User avatar
Hansjoerg
Posts: 117
Joined: Thu Apr 01, 2021 4:17 pm
Answers: 3
x 74
x 62

Re: Gemini for macro code?

Unread post 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
All the "good" news about SWX makes me feel like I'm driving a truck with two trailers straight into a dead end.
Monstrum Mathias
Posts: 15
Joined: Mon Apr 15, 2024 2:03 am
Answers: 0
x 7
x 13

Re: Gemini for macro code?

Unread post 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.
Attachments
Save STEP.swp
(41.5 KiB) Downloaded 27 times
Save parasolid.swp
(43 KiB) Downloaded 29 times
DLZ_SWX_User
Posts: 79
Joined: Mon Dec 20, 2021 1:40 pm
Answers: 0
Location: Thumb Area of Michigan, USA
x 274
x 41

Re: Gemini for macro code?

Unread post 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.
AmenJlili
Posts: 11
Joined: Thu Nov 30, 2023 5:17 pm
Answers: 0
x 15

Re: Gemini for macro code?

Unread post by AmenJlili »

I don’t want to sound like a party pooper, but I would strongly discourage people from using GPTs. Here are a few reasons:
  • 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.
I’ve had more success with general-purpose programming approaches.

(This post was proofread by a GPT.)
User avatar
josh
Posts: 338
Joined: Thu Mar 11, 2021 1:05 pm
Answers: 18
x 25
x 565

Re: Gemini for macro code?

Unread post by josh »

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.
User avatar
Frederick_Law
Posts: 1994
Joined: Mon Mar 08, 2021 1:09 pm
Answers: 8
Location: Toronto
x 1669
x 1515

Re: Gemini for macro code?

Unread post by Frederick_Law »

AI is as good as the data used to train it.
How much SW API code was used to train it? Probably not enough.
User avatar
loeb
Posts: 81
Joined: Sun Jan 16, 2022 5:55 pm
Answers: 1
x 41
x 14

Re: Gemini for macro code?

Unread post by loeb »

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.
User avatar
Frederick_Law
Posts: 1994
Joined: Mon Mar 08, 2021 1:09 pm
Answers: 8
Location: Toronto
x 1669
x 1515

Re: Gemini for macro code?

Unread post by Frederick_Law »

User avatar
loeb
Posts: 81
Joined: Sun Jan 16, 2022 5:55 pm
Answers: 1
x 41
x 14

Re: Gemini for macro code?

Unread post by loeb »

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.
User avatar
AlexLachance
Posts: 2322
Joined: Thu Mar 11, 2021 8:14 am
Answers: 18
Location: Quebec
x 2521
x 2134

Re: Gemini for macro code?

Unread post by AlexLachance »

loeb wrote: Mon Feb 03, 2025 4:03 pm 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.
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.
User avatar
Frederick_Law
Posts: 1994
Joined: Mon Mar 08, 2021 1:09 pm
Answers: 8
Location: Toronto
x 1669
x 1515

Re: Gemini for macro code?

Unread post by Frederick_Law »

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.....
User avatar
loeb
Posts: 81
Joined: Sun Jan 16, 2022 5:55 pm
Answers: 1
x 41
x 14

Re: Gemini for macro code?

Unread post by loeb »

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.
User avatar
Frederick_Law
Posts: 1994
Joined: Mon Mar 08, 2021 1:09 pm
Answers: 8
Location: Toronto
x 1669
x 1515

Re: Gemini for macro code?

Unread post by Frederick_Law »

User avatar
loeb
Posts: 81
Joined: Sun Jan 16, 2022 5:55 pm
Answers: 1
x 41
x 14

Re: Gemini for macro code?

Unread post by loeb »

Can we please leave politics out of our forum?
User avatar
Frederick_Law
Posts: 1994
Joined: Mon Mar 08, 2021 1:09 pm
Answers: 8
Location: Toronto
x 1669
x 1515

Re: Gemini for macro code?

Unread post by Frederick_Law »

Sorry, that wasn't my intention.
Petertha
Posts: 62
Joined: Sun May 26, 2024 3:34 am
Answers: 0
x 14

Re: Gemini for macro code?

Unread post by Petertha »

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.
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.
Post Reply