Opening and refreshing the part family via a macro

Hello

I have a macro that requires a dimension. I then want to open my part family automatically and update the values with this dimension.

Would you know how to open a part family and change the contents of a cell with a value?

Thank you for your feedback

1 Like

Hello

With these two links to API help, I think you'll find the solution.

http://help.solidworks.com/2020/english/api/sldworksapi/get_or_set_whether_edits_update_design_table_example_vb.htm

http://help.solidworks.com/2020/english/api/sldworksapi/get_design_table_example_vb.htm

From the second link, you just have to add a command like swDesTable.SetEntryValue having targeted the column and the row to be modified.

1 Like

Hello Cyril

Thank you very much. Macro 1 is great for verifying that the part family exists.

I used the code from macro 2 to open the room family. The Excell  file opens well, the values indicated are correct but the 3D is not updated. It updates again if I reopen the part family.

 

Here is the code of macro 2 that I indicated:

 

Set swDesTable = swModel.GetDesignTable
bRet = swDesTable.Attach
Debug.Assert bRet
swDesTable.Detach
Part.ForceRebuild

I admit that I don't really understand what these lines are for.... If you know, I'm interested in the info

Could you tell me how to force the reconstruction without having to go back to manual in the family of parts?

A big thank you in advance

Hello

In the snippet of code you give you get the table of the object "swModel" but you ask for a reconstruction on the object "Part", try by putting "swModel.ForceRebuild" instead of "Part.ForceRebuild".

Kind regards

1 Like

Hello

For swDesTable.Detach I admit I don't know exactly what it's for but in the help: Detach the design table from the Microsoft Excel sheet.

In the case of the requested use, I don't think it's necessary.

swDesTable.Attach, on the other hand, is used to activate the table.

Debug.assert is a conditional breakpoint if I don't make a mistake if the boolean bret is false, the macro stops on this line (basically if the table doesn't open).

To update the template you must use swDesTable.UpdateTable 2, True

For more info on the method: 

IDesignTable Interface Members - 2020 - SOLIDWORKS API Help

swDesignTableUpdateOptions_e Enumeration - 2020 - SOLIDWORKS API Help

1 Like

Thank you to both of you for your answers. It works. I still have the excell application (I guess because of the family of parts) which remains open. Would it be possible to close it and come back to the 3D at the end of the macro? Thank you in advance for your feedback

Normally the line swDesTable.UpdateTable 2, True closes the part family and therefore Excel in theory.

 

I confirm that this function closes the part family file but not the Excel application...

 

There must be something else in the macro that's blocking Excel from closing. For my part, I have Excel that disappears from the active processes as soon as the macro ends (but I only have a snippet of code that opens the table, updates a value, closes the table and updates the 3D).

It would take the complete code to see if there is another function that is blocking.

1 Like

Here are the lines...

I made a mix with the models sent....

Thank you for your expertise!

Dim swApp As Object

Dim Part As Object
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long

Sub main()

Set swApp = Application.SldWorks

Set Part = swApp.ActiveDoc
Dim myModelView As Object
Set myModelView = Part.ActiveView

myModelView.FrameState = swWindowState_e.swWindowMaximized


'-----------------------------------
' Preconditions:
' 1. Open a part document that contains a design table.
' 2. Verify that the part document contains a design
   ' table by expanding Tables in the ConfigurationManager.
' 3. Open the Immediate window.
'
' Postconditions:
' 1. Gets whether the design table is updatable before
   ' running the macro.
' 2. Sets the design table to not updatable if it is
   ' updatable or vice versa.
' 3. Examine the Immediate window.
'-----------------------------------
Explicit Option
'Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swDesignTable As SldWorks.DesignTable
Dim boolstatus As Boolean
Dim laststate As Variant
 Dim swDesTable              As SldWorks.DesignTable
 Dim Double Height
 
 

    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swDesignTable = swModel.GetDesignTable()
    ' Allow changes to the characteristics of the
    ' design table
    swDesignTable.EditFeature
    ' Get whether design table is updatable
    laststate = swDesignTable.Updatable
    Debug.Print "Design table updatable: " & CStr(laststate)
    ' If design table is updatable, then set it
    ' to not updatable or vice versa
    swDesignTable.Updatable = Not laststate
    ' Update this characteristic of the design table
    boolstatus = swDesignTable.UpdateFeature()
    ' Get whether design table is updatable
    laststate = swDesignTable.Updatable
    Debug.Print "Design table updatable: " & CStr(laststate)

Height = InputBox("Please indicate the Conveyor Height.... ")
'swModel.GetEquationMgr.Value("Height") = Height
    Set swDesTable = swModel.GetDesignTable
    bRet = swDesTable.Attach
    
'Debug.Assert bRet           'breakpoint if the table does not open, if bret is false   
    swDesTable.UpdateTable 2, True
    swDesTable.Detach
    swModel.ForceRebuild        ' to rebuild a Part.ForceRebuild
    
   Set swApp = Application.SldWorks
   
  
End Sub
 

Hello

Same for me, Excel closes well and disappears from the list of processes at the end of the macro but there is a small period of time before this is done, a time frame that is shorter by deleting the last line "Set swApp = Application.SldWorks" which is useless since this swApp variable is already defined at the beginning of your macro.

I've already noticed that sometimes the Excel process has trouble closing when writing macros and that we often go into debug mode, especially when the macro freezes during execution...

Kind regards

Thank you Roger. Do you have at least one trick to display the active Solidworks window because I have this damn Excel window that takes over.... I'd just like to at least finish my macro by displaying the Sldworks window... Thank you

On the code, I don't see anything in particular, for my part I still have no trace of Excel at the end of the macro.

To put SW back in the foreground, unless I'm mistaken, it's 

bret = swapp.visible

After for the problem itself, either it's a crash of Excel or it's a bug in the Solidworks version.

Is the room family open in another window or is it open in SW?

Another slightly barbaric solution is the shell commands to kill the Excel process:

Shell "taskkill /f /im excel.exe", vbHide

 

Otherwise another idea, it can come from the automatic update of the table when opening, maybe SW is waiting for something in the background (window that would have switched to the background) and suddenly blocks the closure of Excel.

You should test the code with the deactivation of the automatic update of the table, from memory it is in one of the two codes of the help of my first answer.

1 Like

Hello Cyril,

I tried the 1st option... It didn't work. So, I tried the 2nd one and it works perfectly

A big thank you to you

Have a good week

Hello
digging up topic,
I will try to be clear.
This macro works great except that...
1- I modify a sketch
2- I run the macro (which goes well) but at the end it locks all the dimensions of the sketches so it's no longer possible since SW to modify a value.
3- I run the macro again and there I have access to modify the sketches again from SW.
To sum up, if I launch the macro after modifying a sketch, I have to run it a second time to be able to modify a value again.
No matter how much I tinker, I don't find why.
if someone can enlighten me.
May the force be with you.

1 Like

Hello
I am not sure that I have understood the problem properly.
I can be in a sketch or edit directly by double-clicking on the function, but I don't have any dimension lock.
On the other hand, as there is a strong rebuild, we inevitably find ourselves out of the sketch.

Hello
Here is an explanatory video.
I explain what is happening.
1-You can see that I can modify a value at will.
2-I run the macro to update the part
3- I want to modify this same value again but the impossible to modify
4-I rerun the macro and there I have edit access again.
Why do I have to rerun the macro to have access to edit???
May the Force be with you

Hello
I just understood.
In fact, you have to remove this part of the code that changes the table's update behavior.

    Set swDesignTable = swModel.GetDesignTable()
    ' Allow changes to the characteristics of the
    ' design table
    swDesignTable.EditFeature
    ' Get whether design table is updatable
    laststate = swDesignTable.Updatable
    Debug.Print "Design table updatable: " & CStr(laststate)
    ' If design table is updatable, then set it
    ' to not updatable or vice versa
    swDesignTable.Updatable = Not laststate
    ' Update this characteristic of the design table
    boolstatus = swDesignTable.UpdateFeature()
    ' Get whether design table is updatable
    laststate = swDesignTable.Updatable
    Debug.Print "Design table updatable: " & CStr(laststate)

It toggles from "Allow part family to be updated by model changes" to "Disallow model changes that would update part family."
So, first time it switches to the second mode, on the second launch it does the opposite.

2 Likes

@Cyril.f
You're a champion, my champion.
It's nickel a big thank you for this effective solution.
may the force be with you.

2 Likes