Get/Set Assembly Properties from AutoCAD Mechanical by API

By Barbara Han

When creating a new drawing in AcadM 2013 and running the command "_ampartrefedit" with option "_a" (assembly properties). A dialog shows up and the user can add/define properties. Some of our partner need to access to those property data using API.

The assembly property is associated with the model space block table record. So you can first get the model space ID and use API AcmBOMManager::getPartData() to get the assembly property data. This API is provided by SymBBAuto type library. For AutoCAD Mechanical 2013, you need to add reference to SymBBAuto 4.0 type library.

The following are two VBA samples that you can refer to:

Sub setgetAssemblyAttributes()

‘ Set reference to symbol Manager
Dim oSymBBMgr As McadSymbolBBMgr
Set oSymBBMgr = Application.GetInterfaceObject("SymBBAuto.McadSymbolBBMgr")

‘ Set reference to BOM Manager
Dim oBoMMgr As McadBOMMgr
Set oBoMMgr = oSymBBMgr.BOMMgr

‘ Check if the assembly properties have been initialized

‘ Get the assembly attributes from current database modelspace
Dim vdata As Variant
vdata = oBoMMgr.GetPartData(ThisDrawing.ModelSpace)

If VarType(vdata) = vbEmpty Then

‘Create the data to be assigned
‘ Define double array of strings to hold column name and values
Dim sData(0 To 10, 0 To 1) As String
sData(0, 0) = "NOTE": sData(0, 1) = "MyNote"
sData(1, 0) = "STANDARD2": sData(1, 1) = "text for standard2"
sData(2, 0) = "DIM": sData(2, 1) = "10"
sData(3, 0) = "DESCR2": sData(3, 1) = "10"
sData(4, 0) = "PRICE": sData(4, 1) = "100"
sData(5, 0) = "MASS": sData(5, 1) = "101"
sData(6, 0) = "MATERIAL": sData(6, 1) = "IS:2062"
sData(7, 0) = "VENDOR": sData(7, 1) = "XYZ"
sData(8, 0) = "DESCR": sData(8, 1) = "text for description"
sData(9, 0) = "MATERIAL2": sData(9, 1) = "GOLD"
sData(10, 0) = "STANDARD": sData(10, 1) = "text for standard"
‘ Assign the data to current database modelspace
Call oBoMMgr.SetPartData(ThisDrawing.ModelSpace, sData)
Else
‘ print the existing data
Dim j As Integer
For j = LBound(vdata) To UBound(vdata)
Debug.Print vbTab + vdata(j, LBound(vdata, 2)) + vbTab _
+ vbTab + vbTab + vdata(j, UBound(vdata, 2)) + vbCrLf

Next

End If
End Sub

Sub modAssemblyAttributes()

‘ Set reference to symbol Manager
Dim oSymBBMgr As McadSymbolBBMgr
Set oSymBBMgr = Application.GetInterfaceObject("SymBBAuto.McadSymbolBBMgr")

‘ Set reference to BOM Manager
Dim oBoMMgr As McadBOMMgr
Set oBoMMgr = oSymBBMgr.BOMMgr

‘ Get the assembly attributes from current database modelspace
Dim vdata As Variant
vdata = oBoMMgr.GetPartData(ThisDrawing.ModelSpace)
If VarType(vdata) <> vbEmpty Then
‘ Modify material
Dim j As Integer
For j = LBound(vdata) To UBound(vdata)
If vdata(j, LBound(vdata, 2)) = "MATERIAL" Then
vdata(j, UBound(vdata, 2)) = "SILVER"
Exit For
End If
Next
‘ apply modified material
Call oBoMMgr.SetPartData(ThisDrawing.ModelSpace, vdata)
End If

End Sub


Comments

6 responses to “Get/Set Assembly Properties from AutoCAD Mechanical by API”

  1. Thanks Barbara – I ported it to c#:
    using Ac = Autodesk.AutoCAD.Interop;
    using AcCom = Autodesk.AutoCAD.Interop.Common;
    namespace MuM.Base.AutoCAD.ComM
    {
    public class MCadApp
    {
    private Ac.AcadApplication _acApp;
    private Ac.AcadDocument _acDoc;
    private AcadmAuto.AcadmApplication _amApp;
    private AcadmAuto.IMcadUtility _amUtils;
    private SymBBAuto.McadSymbolBBMgr _amSymMgr;
    private SymBBAuto.IMcadBOMMgr _amBomMgr;
    public MCadApp(Ac.AcadApplication comApp, Ac.AcadDocument comDoc)
    {
    _acApp = comApp;
    _acDoc = comDoc;
    _amApp = _acApp.GetInterfaceObject(“AcadmAuto.AcadmApplication”) as AcadmAuto.AcadmApplication;
    _amUtils = _amApp.ActiveDocument.Utility;
    _amSymMgr = _acApp.Application.GetInterfaceObject(“SymBBAuto.McadSymbolBBMgr”) as SymBBAuto.McadSymbolBBMgr;
    _amBomMgr = _amSymMgr.BOMMgr;
    }
    public void SetGetAssemblyAttributes()
    {
    AcCom.AcadModelSpace ms = _acDoc.ModelSpace;
    AcCom.AcadObject modelSpace = ms as AcCom.AcadObject;
    // Check if the assembly properties have been initialized
    // Get the assembly attributes from current database modelspace
    object vdata = _amBomMgr.GetPartData(modelSpace);
    Array arData = vdata as Array;
    if (arData == null)
    {
    // Create the data to be assigned
    // Define double array of strings to hold column name and values
    arData = Array.CreateInstance(typeof(string), 10, 2);
    arData.SetValue(“NOTE”, 0, 0); arData.SetValue(“MyNote”, 0, 1);
    arData.SetValue(“STANDARD2”, 1, 0); arData.SetValue(“text for standard2”, 1, 1);
    arData.SetValue(“DIM”, 2, 0); arData.SetValue(“10”, 2, 1);
    arData.SetValue(“DESCR2”, 3, 0); arData.SetValue(“10”, 3, 1);
    arData.SetValue(“PRICE”, 4, 0); arData.SetValue(“101”, 4, 1);
    arData.SetValue(“MASS”, 5, 0); arData.SetValue(“IS:2062”, 5, 1);
    arData.SetValue(“MATERIAL”, 6, 0); arData.SetValue(“XYZ”, 6, 1);
    arData.SetValue(“VENDOR”, 7, 0); arData.SetValue(“text for description”, 7, 1);
    arData.SetValue(“DESCR”, 8, 0); arData.SetValue(“GOLD”, 8, 1);
    arData.SetValue(“STANDARD”, 9, 0); arData.SetValue(“text for standard”, 9, 1);
    // Assign the data to current database modelspace
    _amBomMgr.SetPartData(modelSpace, arData);
    }
    else
    {
    // print the existing data
    for (int i = arData.GetLowerBound(0); i < arData.GetUpperBound(0); ++i)
    {
    Debug.Print(string.Format(“\t{0}\t{1}”,
    arData.GetValue(i, arData.GetLowerBound(1)),
    arData.GetValue(i, arData.GetUpperBound(1))));
    }
    }
    }
    public void ModAssemblyAttributes()
    {
    AcCom.AcadModelSpace ms = _acDoc.ModelSpace;
    AcCom.AcadObject modelSpace = ms as AcCom.AcadObject;
    // Check if the assembly properties have been initialized
    // Get the assembly attributes from current database modelspace
    object vdata = _amBomMgr.GetPartData(modelSpace);
    Array arData = vdata as Array;
    if (arData != null)
    {
    // Modify material
    for (int i = arData.GetLowerBound(0); i < arData.GetUpperBound(0); ++i)
    {
    if ( arData.GetValue(i, arData.GetLowerBound(0)) == “MATERIAL”)
    {
    arData.SetValue(“SILVER”, i, arData.GetUpperBound(1));
    break;
    }
    }
    _amBomMgr.SetPartData(modelSpace, vdata);
    }
    }
    }
    }

  2. where can i find the file symbbauto.dll??

  3. candace Avatar
    candace

    Hello,
    Is there a way to modify this code to change material Density? Now I can change the assigned material to “SILVER” with the code above, but is there a way to change the Density of the part material?

  4. Matthijs Avatar
    Matthijs

    Hello,
    Is this API still valid in autocad 2019 and is it possible to remove the override on the component name property with this API?
    Greetings,
    Matthijs

  5. I asked this question in the Visual Lisp forum, but got no answer there yet. Hopefully we are able to do that, one way or another!

Leave a Reply to LotfiCancel reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading