How to Retrieve OPM Properties of an AutoCAD Entity in .NET

<?xml encoding=”UTF-8″>By Madhukar Moogala

acad_opm_props

This is a common request from our developers: how to retrieve the properties of an AutoCAD entity that are
displayed in the OPM (Object Property Manager), also known as the Property Palette.

Generally, properties can be retrieved using the get/set methods of a specific entity. However, not all
properties are exposed in .NET, especially dynamic properties that implement OPM’s IDynamicProperty.

Every entity in AutoCAD integrates with the Properties Palette API, which is a combination of ObjectARX and COM
APIs. Custom entities can also define their own properties and participate in the Property Inspector system.

  
//Autodesk.AutoCAD.Internal.PropertyInspector - Internal namespace - //acmgd.dll
public static IDictionary GetOPMProperties(ObjectId id)
{
Dictionary map = new Dictionary();
IntPtr pUnk = ObjectPropertyManagerPropertyUtility.GetIUnknownFromObjectId(id);
if (pUnk != IntPtr.Zero)
{
try
{
using (CollectionVector properties = ObjectPropertyManagerProperties.GetProperties(id, false, false))
{
int cnt = properties.Count();
if (cnt != 0)
{
using (CategoryCollectable category = properties.Item(0) as CategoryCollectable)
{
CollectionVector props = category.Properties;
int propCount = props.Count();
for (int j = 0; j < propCount; j++)
{
using (PropertyCollectable prop = props.Item(j) as PropertyCollectable)
{
if (prop == null)
continue;
object value = null;
if (prop.GetValue(pUnk, ref value) && value != null)
{
if (!map.ContainsKey(prop.Name))
map[prop.Name] = value;
}
}
}
}
}
}
}
finally
{
Marshal.Release(pUnk);
}
}
return map;
}

A test method, to extract Properties of a Solid entity

    
[CommandMethod("GETSOLIDPROPS")]
public static void GetSolidProps()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
PromptEntityOptions entopts = new PromptEntityOptions("nPick a solid entity from the drawing");
entopts.SetRejectMessage("nOnly Solid3d entities are allowed.");
entopts.AddAllowedClass(typeof(Solid3d), false); // Ensure only Solid3d is selectable
PromptEntityResult entityResult = ed.GetEntity(entopts);
if (entityResult.Status != PromptStatus.OK)  // Handle user cancel or error
{
ed.WriteMessage("nOperation canceled or invalid selection.");
return;
}
ObjectId entid = entityResult.ObjectId;
Database db = Application.DocumentManager.MdiActiveDocument.Database;
using (Transaction t = db.TransactionManager.StartOpenCloseTransaction())
{
try
{
Solid3d solid = t.GetObject(entid, OpenMode.ForRead) as Solid3d;
if (solid == null)
{
ed.WriteMessage("nSelected entity is not a valid 3D solid.");
return;
}
var props = GetOPMProperties(entid);
foreach (var prop in props)
{
ed.WriteMessage($"n{prop.Key} : {prop.Value}");
}
t.Commit();
}
catch (System.Exception ex)
{
ed.WriteMessage($"nError: {ex.Message}");
}
}
}


Comments

7 responses to “How to Retrieve OPM Properties of an AutoCAD Entity in .NET”

  1. Hi, thank you so much for sharing.
    Once we get the information, can we change it and re-enter it?

  2. This is read only API, you need to use respective set\get method of an Entity to appear on OPM.

  3. I got problem when using this code working with many 3D solid.
    AutoCAD will crash. Do you have any idea for working with many object in one time ?

  4. Clayton Deanl Avatar
    Clayton Deanl

    pako highway is an exciting action racing game. The game gives players the ultimate thrill with dramatic chases on the highway, where you have to control your car to glide through a series of obstacles and police cars chasing behind.

  5. Thanks for sharing that! This approach is quite common and has been part of our internal reference patterns as well. Great to see it documented in various places, no surprise it came from Tony. He’s one of the top contributors in the AutoCAD community!

  6. caddzooks Avatar
    caddzooks

    Thanks for the complement (I’m Tony). I should point out that credit for the version I linked to goes to Cyrille Fauvel, who many moons ago posted a rudimentary example showing how to use the classes involved. All I really did was polish it up and package it in an easily-consumable form.
    And, thanks for that minor improvement with the try/catch.

Leave a Reply to Madhukar MoogalaCancel reply

Discover more from Autodesk Developer Blog

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

Continue reading