assembly context and component property

Programming and macros
User avatar
mp3-250
Posts: 630
Joined: Tue Sep 28, 2021 4:09 am
Answers: 20
Location: Japan
x 702
x 347

assembly context and component property

Unread post by mp3-250 »

I cannot post my sample code as my workplace has no access to this site and I am not allowed to upload company data from my office.
apologize for the inconvenience.

I used the code below inside my macro, but I noticed that when editing a component it sometimes try to return value from the assy instead of the component I selected and set to edited in context with my macro.

Code: Select all


'**********************
'Copyright(C) 2023 Xarial Pty Limited
'Reference: https://www.codestack.net/solidworks-api/data-storage/custom-properties/
'License: https://www.codestack.net/license/
'**********************

Dim swApp As SldWorks.SldWorks

Sub main()

    Set swApp = Application.SldWorks
    
    Dim swModel As SldWorks.ModelDoc2
    
    Set swModel = swApp.ActiveDoc
    
    Debug.Print GetPropertyValue(swModel, "Part Number")
    Debug.Print GetPropertyValue(swModel, "Revision")
    
End Sub

Function GetPropertyValue(model As SldWorks.ModelDoc2, prpName As String) As String
    
    Dim prpVal As String
    Dim swCustPrpMgr As SldWorks.CustomPropertyManager
    
    If TypeOf model Is SldWorks.PartDoc Or TypeOf model Is SldWorks.AssemblyDoc Then
        Set swCustPrpMgr = model.ConfigurationManager.ActiveConfiguration.CustomPropertyManager
        swCustPrpMgr.Get4 prpName, True, "", prpVal
    End If
    
    If prpVal = "" Then
        Set swCustPrpMgr = model.Extension.CustomPropertyManager("")
        swCustPrpMgr.Get4 prpName, True, "", prpVal
    End If
    
    GetPropertyValue = prpVal
    
End Function

I used the getproperty function and I would like to know the logical steps to correctly use it.

codestack made an explanation about redirecting pointers, and the code is behaving strangely so my pointers are probably mixed up, but I am not able to follow the whole explaination.

a simple step by step explation would help me a lot. I cannot connect dots.

My objective is editing a component from an assy (1 component pre selected from the tree) I edit it in context, select a sketch Inside it and put in edit mode, read a property (with the function above) from the same component, edit the sketch with that value from the property and return to the assy.

https://www.codestack.net/solidworks-ap ... y/context/
User avatar
josh
Posts: 295
Joined: Thu Mar 11, 2021 1:05 pm
Answers: 16
x 22
x 508

Re: assembly context and component property

Unread post by josh »

From the Help documentation for ActiveDoc:
The currently active document cannot be the document that is being edited by the end-user. For example, you can use in-context editing to modify an assembly component. The currently active document is the assembly, but the edit target is the assembly component. For assemblies, you can determine the edit target by using the IAssemblyDoc::GetEditTarget or IAssemblyDoc::IGetEditTarget2 method.
User avatar
mp3-250
Posts: 630
Joined: Tue Sep 28, 2021 4:09 am
Answers: 20
Location: Japan
x 702
x 347

Re: assembly context and component property

Unread post by mp3-250 »

the use of edittarget should be straight forward, but I made it work for writing a property sub, but not for the reading function. could it be problem related to pass a variable from the main program to the function or sub?
must be invoked before the function?
pass the argument to the function and invoke it from there?
Post Reply