Edit Part Ownership from Assembly

Hello

I'm looking for a way to edit the custom properties of a part from the assembly.

I found this little macro on the web, but it's just a viewer.

I also have a few macro templates that open or save the selected components.

I thought I would easily find in the code the "selected component" and "open" or "save" parts to replace with "open props. personalized", but they are written differently, there are counters, lots of things that I don't understand, in short, no, it's not easy...

 

 


view_properties_of_selected_component_from_assembly.swp

Hello

Here is a small example (made in a hurry) that should help you:

Option Explicit

Dim swApp As SldWorks.SldWorks
Dim Model As ModelDoc2
Dim CompModel As ModelDoc2
Dim swChildComp As SldWorks.Component2
Dim SelectedObject As Object
Dim NbrSelections As Long
Dim i As Long
Dim lErrors As Long
Dim lWarnings As Long

Dim swModel As SldWorks.ModelDoc2
Dim bRet As Boolean
Dim myAssy As AssemblyDoc
Dim nInfo As Long
Dim swModelDocExt As ModelDocExtension
Dim swCustProp As CustomPropertyManager
Dim val As String
Dim valout As String

Sub main()

    Set swApp = Application.SldWorks
    Set Model = swApp.ActiveDoc
    
    Set myAssy = Model

    Dim SelMgr As SelectionMgr
    Set SelMgr = Model.SelectionManager
        
    NbrSelections = SelMgr.GetSelectedObjectCount2(-1)

    Set SelectedObject = SelMgr.GetSelectedObject6(1, -1)
    If NbrSelections = 1 Then
        Set swChildComp = SelectedObject
        Set CompModel = swChildComp.GetModelDoc2
        
        bRet = myAssy.EditPart2(True, True, nInfo)
        Set swModel = myAssy.GetEditTarget
        Set swModelDocExt = swModel.Extension
        Set swCustProp = swModelDocExt.CustomPropertyManager("")
        
        bRet = swCustProp.Add3("MaPropriete", swCustomInfoType_e.swCustomInfoText, "Ma valeur 1", swCustomPropertyAddOption_e.swCustomPropertyDeleteAndAdd)
        bRet = swCustProp.Get4("MaPropriete", False, val, valout)
        
        Debug.Print "valeur évaluée : " & valout
        
        bRet = swCustProp.Set2("MaPropriete", "Ma valeur 3")
        bRet = swCustProp.Get4("MaPropriete", False, val, valout)
        
        Debug.Print "valeur évaluée : " & valout
        
        bRet = swModel.Save3(swSaveAsOptions_Silent, lErrors, lWarnings)
        
        myAssy.EditAssembly
    End If
    
End Sub

To test, you open an assembly and then select a part in the feature manager before launching the macro (to be done on test parts).

Kind regards

1 Like

Thank you d.roger

I tried your macro, it works, but it's not exactly what I expected (in fact it goes a little too far)

in fact your macro

- Activate the selected component : ok for me

- created the propriété "Ma propriété", avec la vale " Ma valeur 1" : en fait je voudrais juste ouvrir la fenêtre de propriété personnalisés.

- enregistre (?) et ferme le composant pour retourner a l'assemblage : Idéalement cette opération peut être déclanché à la fermeture de la fenêtre de propriété personnalisés. si non, a faire manuellement, ça sera déjà bien.

 

 

 

Something like this:

Option Explicit

Dim swApp As SldWorks.SldWorks
Dim Model As ModelDoc2
Dim CompModel As ModelDoc2
Dim swChildComp As SldWorks.Component2
Dim SelectedObject As Object
Dim NbrSelections As Long
Dim i As Long
Dim lErrors As Long
Dim lWarnings As Long

Dim swModel As SldWorks.ModelDoc2
Dim bRet As Boolean
Dim myAssy As AssemblyDoc
Dim nInfo As Long

Sub main()

    Set swApp = Application.SldWorks
    Set Model = swApp.ActiveDoc
    
    Set myAssy = Model

    Dim SelMgr As SelectionMgr
    Set SelMgr = Model.SelectionManager
        
    NbrSelections = SelMgr.GetSelectedObjectCount2(-1)

    Set SelectedObject = SelMgr.GetSelectedObject6(1, -1)
    If NbrSelections = 1 Then
        Set swChildComp = SelectedObject
        Set CompModel = swChildComp.GetModelDoc2
        
        bRet = myAssy.EditPart2(True, True, nInfo)
        Set swModel = myAssy.GetEditTarget

        swModel.FileSummaryInfo
        
        bRet = swModel.Save3(swSaveAsOptions_Silent, lErrors, lWarnings)
        
        myAssy.EditAssembly
    End If
    
End Sub

Kind regards

2 Likes

ALLELUIA ! glory to you, on land as well as on sea!

I feel that my colleagues will like it!

Miles thank you D.Roger, always so efficient

1 Like

I still have a very small remark: it doesn't work properly on the sub-assemblies:

It looks like there is a reversal between the steps "open the properties window" and "activate the selected component"

 

Hello

Try with this macro version:

Option Explicit

Dim swApp As SldWorks.SldWorks
Dim Model As ModelDoc2
Dim CompModel As ModelDoc2
Dim swChildComp As SldWorks.Component2
Dim SelectedObject As Object
Dim NbrSelections As Long
Dim i As Long
Dim lErrors As Long
Dim lWarnings As Long

Dim swModel As Object
Dim bRet As Boolean
Dim myAssy As AssemblyDoc
Dim nInfo As Long

Sub main()

    Set swApp = Application.SldWorks
    Set Model = swApp.ActiveDoc
    
    Set myAssy = Model

    Dim SelMgr As SelectionMgr
    Set SelMgr = Model.SelectionManager
        
    NbrSelections = SelMgr.GetSelectedObjectCount2(-1)

    Set SelectedObject = SelMgr.GetSelectedObject6(1, -1)
    If NbrSelections = 1 Then
        Set swChildComp = SelectedObject
        Set CompModel = swChildComp.GetModelDoc2
        
        If CompModel.GetType = swDocumentTypes_e.swDocASSEMBLY Then
            myAssy.EditAssembly
        End If

        bRet = myAssy.EditPart2(True, True, nInfo)
        Set swModel = myAssy.GetEditTarget
    
        swModel.FileSummaryInfo
            
        bRet = swModel.Save3(swSaveAsOptions_Silent, lErrors, lWarnings)
            
        myAssy.EditAssembly

    End If
    
End Sub

Kind regards

1 Like

Awesome. Nothing to complain about.

This macro somewhat compensates for the bug of the visualization tool mentioned in this post

Thank you very much!

1 Like