SolidWorks Macro and Equation

Hello

I have a small problem with the creation of a macro with which I was hoping to use a global variable defined when the part file was created. My disappointment was great.
I am attaching a description of the problem found in the attached document.
pb macro.pdf (138.3 Kb)
I tried to bias as with parameterized curves, by first creating a sketch whose dimension had the global variable as its value and then asking the macro to take this dimension. I came up empty.

Thank you for your help and have a very good day

I redid the file describing the problem so that it is more explicit.
Thank you and have a good Sunday
pb macro.pdf (234.3 KB)

Good evening

Why use a macro? I do this via a form. I enter all my dimensions and the part generates and modifies it accordingly. I'll give you an example of a piece when I'm in front of the computer.

1 Like

Here is a tray that can be set up via the custom properties that I had made, hoping that it can help you.
image
Bac.SLDPRT (135.1 KB)

Good evening ac_cobra _427,
Thank you for your answer. I am well aware of your proposal, which I use extensively because it is very relevant.
In my case, if I want a macro it is because it would save me a lot of clicks. I do laser cutting of plywood. So I have a template (*.dot) that has a plywood texture and a global variable representing thickness. I draw my parts to send them to the cutting table in dxf format. I could be satisfied with making only the sketches but to have a global view of the product and to ensure the assembly (assembly, kinetic simulation, etc.) I model them in 3D. This modeling is based only on mid-plane extrusions, with the thickness as a value, hence the interest that it is as a global variable and thus accessible to the equations. If I could do these extrusions in one click, it would be more comfortable for me because it quickly becomes repetitive. So I'd like to have a macro with its icon that I'll put in the functions banner, and hop click click it's over.

Hello

I probably misunderstood but if you have a global variable and its value 4 is in agreement with the extrusion value, why a global variable??

If the goal is to be able to change this thickness value later, it's faster to change the dimension directly with:
click on the extrusion in the shaft + click on the dimension + 4 + enter ",
while to modify a variable you must:
Open the equations + click on the variable + 4 + enter to validate the variable + enter or click to validate and close the equations window".

A variable costs 1 click more than via the quote.

Hello

The problem for the macro is the fact that you can't link the extrusion to the thickness variable outside of a sheet metal function.
Maybe try by getting the value of the thickness dimension and putting a text of type " =Thickness "

Hello

Try this macro, it should work.

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swSketch As SldWorks.SketchManager
Dim swFeature As SldWorks.Feature
Dim swDimension As SldWorks.Dimension
Dim userInput As String
Dim newLength As Double
Dim newWidth As Double
Dim newHeight As Double

Sub main()
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    
    If swModel Is Nothing Then
        MsgBox "Aucune pièce ouverte.", vbExclamation, "Erreur"
        Exit Sub
    End If
    
    ' Demande les nouvelles dimensions
    userInput = InputBox("Entrez la longueur (en mm) :", "Modification de la pièce")
    If IsNumeric(userInput) Then newLength = CDbl(userInput) / 1000 Else Exit Sub

    userInput = InputBox("Entrez la largeur (en mm) :", "Modification de la pièce")
    If IsNumeric(userInput) Then newWidth = CDbl(userInput) / 1000 Else Exit Sub

    userInput = InputBox("Entrez la hauteur (en mm) :", "Modification de la pièce")
    If IsNumeric(userInput) Then newHeight = CDbl(userInput) / 1000 Else Exit Sub

    ' Sélection de l'esquisse "Esquisse1"
    Set swFeature = swModel.FeatureByName("Esquisse1")
    If swFeature Is Nothing Then
        MsgBox "L'esquisse 'Esquisse1' n'a pas été trouvée.", vbExclamation, "Erreur"
        Exit Sub
    End If

    swFeature.Select2 False, 0
    swModel.EditSketch

    ' Mise à jour des dimensions
    Set swDimension = swModel.Parameter("D1@Esquisse1")
    If Not swDimension Is Nothing Then swDimension.SystemValue = newLength
    
    Set swDimension = swModel.Parameter("D2@Esquisse1")
    If Not swDimension Is Nothing Then swDimension.SystemValue = newWidth
    
    Set swDimension = swModel.Parameter("D1@Boss.-Extru.1")
    If Not swDimension Is Nothing Then swDimension.SystemValue = newHeight

    ' Validation de l'esquisse et mise à jour
    swModel.EditRebuild3
    swModel.SetSaveFlag
    swModel.EditSketch
    
    MsgBox "Dimensions mises à jour avec succès.", vbInformation, "Succès"
End Sub


Diension piece.swp (40 KB)

2 Likes

Good evening @pierre_mihailovic1 ,

With the attached macro, extrusion in two mouse clicks of an existing sketch, by midplane and thickness managed by a global variable:

  • the first click to select the sketch;
  • the second to run the macro (if a button exists).

The global variable is defined in the macro by a constant named " Thick ". It must of course exist in the document (or better, in the template). PRTDOT). The extrusion thickness rating does refer to the overall variable.

Note: Attempting to use the " Thickness " notation results in errors in the equation manager. Perhaps it is a word reserved for sheet metal.

ExtrusionEpais.zip (76.7 KB)

3 Likes

Yes, it seems to me that this term is reserved for sheet metal. I think I've already encountered this problem!

1 Like

Hello

Yes it's related to the sheet metal, however in " normal " operation when you select link to thickness on an extrusion function it still takes this global variable into account. On the other hand, it cannot be modified, it can only be changed by setting up a sheet metal function.
Hence, I think, the fact that this function is not accessible via macro outside of a sheet metal.

2 Likes

Thank you to all of you. I'm going to look at the macro proposed by ac_cobra_427.
I work a lot with global variables driven by an external file. This allows me for an assembly with N parts all laser cut in a plywood of common thickness to modify all the overlaps in one go.
Have a nice day