Using the Visualization Cube in an Assembly

Hello everyone,

In an assembly, is it possible to create a visualization cube for all the parts present in the same assembly?

because I have the impression that you have to edit each piece to create the cube.

Is there a way to do it or to automate by a vba loop the creation of a visualization cube for each room.

Thank you in advance.

Hello

To create it on the parts, by macro as well as manually, you have to edit each part.

But it's faster and simpler by macro since it's done in one click.

Kind regards

Hello and thank you d.roger for your intervention,

I code in VBA for Excel, but VBA for Solidworks is a problem for me.

I tried to modify a code opening all the parts of an assembly and to create the visualization cube each time I opened, but it doesn't create a cube for me.

And I don't see where it is wrong.

In case I put the macro if someone sees where the problem is.


remplprop.swp

You create the cube on the Part variable with this line "Set BoundingBox = Part. FeatureManager.InsertGlobalBoundingBox(swGlobalBoundingBoxFitOptions_e.swBoundingBoxType_BestFit, False, False, longstatus)" except the "Part" variable is attached to your assembly since it's the "swApp.ActiveDoc" at the beginning so when I launch your macro the cube is created on the assembly. You have to create your cube on the variable "swModel" which is the part you open  by the action of the line "Set swModel = swApp.ActivateDoc(Document)" so a first modification is to correct, at least, the lines as follows:

If Not CmpDoc.GetPathName Like "*\AppData\*" Then

    T = CmpDoc.CustomInfo("Type")
    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

This macro then becomes functional...

Kind regards

As on the other discussion:

 

And put your coins in solved mode otherwise it won't work...

Kind regards

1 Like

Thank you for this first step, on the other hand I have 7 parts in my assembly and there are only 4 parts that have a visualization cube the last 3 don't.

There must be  a problem in the loop, that it would not take into account all the parts?

Sorry but what is the solved mode, excuse my ignorance.

When loading an assembly in Solidworks it is possible to either open the parts in resolved or light mode, the difference is that either the parts are actually open or only a graphical visualization of them is displayed on the screen. See HERE for the benefits of the resolved mode and HERE for lightweight parts.

So right-click on the assembly and select "Set lightweight parts to Resolved" in the context menu and then try your macro again.

Kind regards

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?