Using the Visualization Cube in an Assembly

Hello

I don't see any reason why this macro makes you crash SW, all the functions used are prior to SW2018. Put numbered MsgBoxes between each step to see what function it crashes at...

Kind regards

Hello

Here's the line that crashes Solidworks:

 bRet = swModel.Save3(swSaveAsOptions_Silent, swErrors, swWarnings)

 

Hello

As a reminder, the macros given here are only for example and must be reworked to at least add error handling...

So you have to add this error handling (see HERE) in the For loop and escalate, if you wish, this error in the swErrors argument (see HERE) of the Save3 function .

Kind regards

Excuse me, but it's not the line I quoted above that makes me crash Solidworks, I just redid some tests and it's ultimately this one:

Set BoundingBox = swModel.FeatureManager.InsertGlobalBoundingBox(swGlobalBoundingBoxFitOptions_e.swBoundingBoxType_BestFit, False, False, longstatus)

However I don't understand because it's the same code that worked in the previous macro.

And thank you for your clarifications on Save3, I'll look into it.

The answer is pretty much the same as before: insert the error handling and read the error type in the longstatus variable, see HERE for the possible return error type.

Hello

I tried to handle the error, but still the same problem, i.e. Solidworks crashes on the line:

Set BoundingBox = swModel.FeatureManager.InsertGlobalBoundingBox(swGlobalBoundingBoxFitOptions_e.swBoundingBoxType_BestFit, False, False, longstatus)

Despite the error handling, Solidworks crashes without opening the msgbox, is there a problem with the code of my error handling, because the msgbox should appear if an error is detected.

So there is no error detected, so what can be a problem?

Here is the code with error handling:

Option Explicit

Dim swApp As Object
Dim longstatus As Long
Dim swModel As SldWorks.ModelDoc2
Dim bRet As Boolean
Dim swErrors As Long
Dim swWarnings As Long
Dim i As Long
Dim Assembly As ModelDoc2
Dim myAssy As AssemblyDoc
Dim myCmps As Variant
Dim myCmp As Component2
Dim nInfo As Long
'Public Enum swGlobalBoundingBoxResult_e
'Inherits System.Enum
   

Sub main()
    Set swApp = Application.SldWorks
    Set Assembly = swApp.ActiveDoc
    Set myAssy = Assembly
'Dim instance As swGlobalBoundingBoxResult_e
    myCmps = myAssy.GetComponents(False)
    For i = 0 To UBound(myCmps)
        Set myCmp = myCmps(i)
        If (myCmp.GetSuppression = 3) Or (myCmp.GetSuppression = 2) Then
            bRet = myCmp.Select2(False, 0)
            bRet = myAssy.EditPart2(True, True, nInfo)
            Set swModel = myAssy.GetEditTarget
            
On Error GoTo errorHandler

            Dim BoundingBox As Object
            Set BoundingBox = swModel.FeatureManager.InsertGlobalBoundingBox(swGlobalBoundingBoxFitOptions_e.swBoundingBoxType_BestFit, False, False, longstatus)

            bRet = swModel.Save3(swSaveAsOptions_Silent, swErrors, swWarnings)

            myAssy.EditAssembly
        End If
    Next i
    
    Assembly.ForceRebuild3 True
    
    MsgBox "Cubes créés", vbExclamation
Exit Sub
    
errorHandler:
    'indique si l'erreur est detecte
    MsgBox "erreur"
End Sub

 

Hello

 No opinion on my error handling is it correct or not at all?

Thanks in advance

 

Hello @treza88 

[HS On]

Small question out of pure curiosity ;-)

What good is it for you to have a visualization cube for a room and especially what is the use in an ASM to have a visualization cube for each room.
Personally, I never use the visualization cube.

[HS /Off]

Kind regards

Thank you Zoro_mp for your question,

For me, and this may be silly, it was the way to retrieve the information of the dimensions of my parts, i.e. length, width, and thickness.

But as mentioned before, it may be silly, there may be other ways.

Hello everyone and thank you for this Macro! 

I can't modify the code to do this on a unique piece. I actually have a library of 3D parts and I would like to put a cube on each part. Except that I have to do it one by one. And via "integration" on Mycadtools I could use a macro that will do the job. 

So I wanted to know if we could modify this macro just to make the cube on a single piece

Thank you for your help!