Using the Visualization Cube in an Assembly

Ok thank you for the clarifications.

And I was well in resolution and I still only have 4 out of 7 pieces that have a visualization cube.

When I launch the modified macro on a larger assembly it creates all the visualization cubes, don't you have any surface parts? and which version of SW?

No I don't have any surface parts and my version is a 2018

I just did some tests again and still the same here is my assembly if you want to do some tests


mb_entree.zip

I did a test on another assembly with 7 parts and there it works, I don't know why it doesn't work with this one.

Maybe you'll see something on your side.

Can you tell me if this code works on symmetrical parts or with a component repeat.

I'll do some tests on my side to check, but I'm interested in your opinion 

I get the visualization cube on all the parts of your assembly.

I've modified the code like the following to remove a whole bunch of lines that probably don't serve any purpose:

Dim swApp As Object
'Dim Part As Object
Dim longstatus As Long, longwarnings As Long
Dim swModel             As SldWorks.ModelDoc2
Dim bRet                As Boolean
Dim swErrors            As Long
Dim swWarnings          As Long
Dim i                   As Long
'Dim j                   As Long
'Dim cCnt                As Long
Dim Assembly            As ModelDoc2
Dim myAsy               As AssemblyDoc
Dim myCmps
Dim CmpDoc              As ModelDoc2
Dim myCmp               As Component2
'Dim tCmp                As Component2

Sub main()
    'Dim myModelView As Object

    Set swApp = Application.SldWorks
    'Set Part = swApp.ActiveDoc
    Set Assembly = swApp.ActiveDoc
    Set myAsy = Assembly

    myCmps = myAsy.GetComponents(False)
    For i = 0 To UBound(myCmps)
        Set myCmp = myCmps(i)
        If (myCmp.GetSuppression = 3) Or (myCmp.GetSuppression = 2) Then
            'cCnt = 0
            Set CmpDoc = myCmp.GetModelDoc
            'Cfg = myCmp.ReferencedConfiguration
            
            'compte le nombre d'occurences des composants
            'For j = 0 To UBound(myCmps)
            'Set tCmp = myCmps(j)
                'If tCmp.GetSuppression <> 0 Then
                    'If tCmp.GetModelDoc2 Is CmpDoc Then
                        'If tCmp.ReferencedConfiguration = Cfg Then
                            'cCnt = cCnt + 1
                        'End If
                    'End If
                'End If
            'Next j
            Document = CmpDoc.GetPathName
            'remplissage propriété Type
            
            'If Not CmpDoc.GetPathName Like "*\AppData\*" Then
            
                'T = CmpDoc.CustomInfo("Type")
                'Debug.Print T
                'If T = "" Then
                    Set swModel = swApp.ActivateDoc(Document)
                    'swModel.ForceRebuild                        ' Reconstruction
                    'swModel.ShowNamedView2 "*Isométric", -1     ' Vue Isométrique
                    'swModel.ViewDisplayShaded                   ' Vue Ombrée
                    'swModel.ViewZoomtofit2                      ' Zoom au mieux
                    Dim BoundingBox As Object
                    Set BoundingBox = swModel.FeatureManager.InsertGlobalBoundingBox(swGlobalBoundingBoxFitOptions_e.swBoundingBoxType_BestFit, False, False, longstatus)
             
                    'Part.GraphicsRedraw2
                    'Dim BoundingBox As Object
                    'Set BoundingBox = Part.FeatureManager.InsertGlobalBoundingBox(swGlobalBoundingBoxFitOptions_e.swBoundingBoxType_BestFit, False, False, longstatus)
                    'Part.ClearSelection2 True
                    
                
                    'Enregistre et ferme le document actif en mode silencieux
                    bRet = CmpDoc.Save3(swSaveAsOptions_Silent, swErrors, swWarnings)
                    swApp.CloseDoc (Document)
                'Else
                'End If
            'End If
        End If
    Next i
    
    MsgBox "Cube créé", vbExclamation

End Sub

 

3 Likes

Ok thank you for your feedback, so I don't understand why at home I have problems, but I may have an explanation later.

Thank you for your cleaning up of the code, I also worked on my side to remove what is not useful, but I will compare with yours.

In the meantime, a big thank you for your help.

If I have a problem I'll come back to the thread in the meantime I'll validate your last post

Another question that bothers me, we open all the pieces, but in an assembly we can edit without opening and it saves time.

Is it possible to do the same thing with this macro?

PS: your code was much more refined than mine.

Hello

Yes, it's possible with the EditPart2 function, but be careful not to get mixed up between the ModelDoc2AssemblyDoc and Component2.

Kind regards

Hello

Do you have something that allows me to understand the difference between these functions?

Because my research is too vague to find my way around.

Hello

Here's an example that should do the same thing as the macro already provided:

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

Sub main()
    Set swApp = Application.SldWorks
    Set Assembly = swApp.ActiveDoc
    Set myAssy = Assembly

    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

            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

End Sub

Kind regards

Good evening

I just tried your code, but it makes me crash Solidworks 2018.

Without putting me any elements of why he crashed.

Can you guide me?

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