.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Definition of Dynamic Blocks with .NET API

13 REPLIES 13
Reply
Message 1 of 14
uomopinza
4658 Views, 13 Replies

Definition of Dynamic Blocks with .NET API

Hello, I'd like to know if it is possible, with the .NET API provided in AutoCAD 2009, to define dynamic blocks (dynamic properties and actions) completely from .NET code (VB.NET or C#).

If yes, can someone provide me a tutorial about this issue ?

Thank you.
13 REPLIES 13
Message 2 of 14
Anonymous
in reply to: uomopinza


Unfortunately no, the authoring elements were not publicly
exposed.


 

AcadXTabs: MDI Document Tabs for AutoCAD 2009
Supporting AutoCAD 2000
through 2009

href="http://www.acadxtabs.com">http://www.acadxtabs.com

 


 

 


style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
Hello,
I'd like to know if it is possible, with the .NET API provided in AutoCAD
2009, to define dynamic blocks (dynamic properties and actions) completely
from .NET code (VB.NET or C#). If yes, can someone provide me a tutorial about
this issue ? Thank you.
Message 3 of 14

Is it possible to show the dynamic properties?

I can retreive AttributeDefinitions, but no properties...
Message 4 of 14
bobbydehero
in reply to: uomopinza

sample code for iterating all dynamic blkrefs of MS and accessing their prop's:

using (BlockTable bt = (BlockTable)trans.GetObject(HostApplicationServices.WorkingDatabase.BlockTableId, OpenMode.ForRead, false))
{
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead);
{
foreach(ObjectId objId in btr)
{
using (DBObject dbObj = trans.GetObject(objId, OpenMode.ForRead, false))
{
if (dbObj != null && dbObj.GetType() == typeof(BlockReference))
{
try
{
BlockReference br = dbObj as BlockReference;
if (br != null && br.IsDynamicBlock)
{
DynamicBlockReferencePropertyCollection dynBlkRefProps = br.DynamicBlockReferencePropertyCollection;
foreach (DynamicBlockReferenceProperty dynProp in dynBlkRefProps)
{
// ... access dynprop methods
// dynProp... PropertyName, Value, GetAllowedValues()
}
}
}
catch (System.Exception ex)
{
// ...
}
}
}
}
}
}
Message 5 of 14

Hi bobbydehero,

Thanks for your answer. However, this is not what I am looking for.

What you are describing, is how to get the PropertyREFERENCES from a BlockREFERENCE.

What I need to do is find the PropertyDEFINITIONS from a BlockDEFINITION.

I can extract the AttributeDefinitions from a blockDefinitions by iterating it's entities, but I still have not found how to extract the Properties...
Message 6 of 14
bobbydehero
in reply to: uomopinza

Ok, I'm not sure whether this suggestion works, before I've tried myself... but you might find something if you do this:

1) try querying the BlockTableRecord (that is dynamic) extension dictionary
2) get the "ACAD_ENHANCEDBLOCK" entry and examine the resbuf's - maybe you can find something there.
Message 7 of 14

Hi, thanks, I'll try that.

For now, we'll create a BlockReference in memory and access the Properties that way...
Message 8 of 14
bobbydehero
in reply to: uomopinza

Yeah, that's probably wise... I ran a quick test and found out that the ACAD_ENHANCEDBLOCK dictionary entry is an EvalGraph object that is not exposed in the .Net framework (Autodesk.AutoCAD.DatabaseServices.EvalGraph).

I then tried C++ and the corresponding AcDbEvalGraph class is accessible for 3rd party dev's, but the implementation of the node classes are still not public - meaning you can only access them as AcDbObjects, here's a printout of the class descriptor names (for my sample dynamic block table record):
AcDbBlockStretchAction
AcDbBlockMoveAction
AcDbBlockVisibilityParameter
AcDbBlockVisibilityGrip
AcDbBlockGripExpr
AcDbBlockGripExpr
AcDbBlockLookupParameter
AcDbBlockLookupAction
AcDbBlockLinearParameter
AcDbBlockLinearGrip
Message 9 of 14
bobbydehero
in reply to: uomopinza

bertvan -

It's actually possible to read the dynamic properties from a BlockTableRecord if you for some reason still want to...
Here's some sample code, and you'll need to reference the unsupported acmgdinternal module for it (generally not recommended):

Database db = HostApplicationServices.WorkingDatabase;
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)(trans.GetObject(db.BlockTableId, OpenMode.ForRead));
if (bt.Has(sDynBlk) == true)
{
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[sDynBlk], OpenMode.ForRead);
if (btr.IsDynamicBlock)
{
try
{
DBDictionary extDictDynBT = (DBDictionary)trans.GetObject(btr.ExtensionDictionary, OpenMode.ForRead, false);
DBObject acadEnhancedBlk = (DBObject)trans.GetObject(extDictDynBT.GetAt("ACAD_ENHANCEDBLOCK"), OpenMode.ForRead, false);
EvalGraph graph = null;
ObjectId idGraph = acadEnhancedBlk.ObjectId;
if (!idGraph.IsNull)
graph = (EvalGraph)trans.GetObject(idGraph, OpenMode.ForRead);

if (graph != null)
{
int [] iNodes = graph.GetAllNodes();
foreach(int iNode in iNodes)
{
DBObject node = graph.GetNode((uint)iNode, OpenMode.ForRead, trans);
if (node != null)
{
EvalExpr eExpr = node as EvalExpr;
if (eExpr.GetType() == typeof(BlockLookupAction))
{
BlockLookupAction blua = eExpr as BlockLookupAction;
if (blua != null)
{
Array pDataTable;
LookupColumnDescriptorCollection descArray;
blua.GetLookupTable(out pDataTable, out descArray);
if (pDataTable != null)
{
....


if (eExpr.GetType() == typeof(BlockLookupParameter))
{
BlockLookupParameter blup = eExpr as BlockLookupParameter;
if (blup != null)
{
BlockParameterPropertyDescriptorCollection props = blup.PropertyDescription;
foreach (BlockParameterPropertyDescriptor prop in props)
{
....
Message 10 of 14
cean_au
in reply to: uomopinza

just wondering if it can be done now?

Message 11 of 14
Hallex
in reply to: cean_au

I hope bobbydehero will not make a sense on me..

Try this slightly eedited code

{code}

        [CommandMethod("DynamicPropertiesTest","dyntest", CommandFlags.Modal )]
        public static void TestDynProps()
        {

            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

            Database db = doc.Database;

            Editor ed = doc.Editor;
           
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                BlockReference bref = trans.GetObject(ed.GetEntity("\nSelect dynamic block").ObjectId, OpenMode.ForRead) as BlockReference;
                if (bref == null)
                {
                    Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("Nothing or wrong object selected");
                    return;
                }
               
                if (!bref.IsDynamicBlock)
                {
                    Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("selected is not dynamick block");
                    return;
                }
                BlockTable bt = (BlockTable)(trans.GetObject(db.BlockTableId, OpenMode.ForRead));

                BlockTableRecord btrec = trans.GetObject(bref.DynamicBlockTableRecord, OpenMode.ForRead,false) as BlockTableRecord;
                string sDynBlk = btrec.Name;
                if (bt.Has(sDynBlk) == true)
                {
                    BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[sDynBlk], OpenMode.ForRead);
                    if (btr.IsDynamicBlock)
                    {
                        try
                        {
                            DBDictionary extDictDynBT = (DBDictionary)trans.GetObject(btr.ExtensionDictionary, OpenMode.ForRead, false);
                            DBObject acadEnhancedBlk = (DBObject)trans.GetObject(extDictDynBT.GetAt("ACAD_ENHANCEDBLOCK"), OpenMode.ForRead, false);
                            EvalGraph graph = null;
                            ObjectId idGraph = acadEnhancedBlk.ObjectId;
                            if (!idGraph.IsNull)
                                graph = (EvalGraph)trans.GetObject(idGraph, OpenMode.ForRead);

                            if (graph != null)
                            {
                                int[] iNodes = graph.GetAllNodes();
                                foreach (int iNode in iNodes)
                                {
                                    DBObject node = graph.GetNode((uint)iNode, OpenMode.ForRead, trans);
                                    if (node != null)
                                    {
                                        EvalExpr eExpr = node as EvalExpr;
                                        if (eExpr.GetType() == typeof(BlockLookupAction))
                                        {
                                            BlockLookupAction blua = eExpr as BlockLookupAction;
                                            if (blua != null)
                                            {
                                                Array pDataTable;
                                                LookupColumnDescriptorCollection descArray;
                                                blua.GetLookupTable(out pDataTable, out descArray);
                                                if (pDataTable != null)
                                                {
                                                    foreach (object obj in pDataTable)
                                                    {
                                                        ed.WriteMessage("\n\t{0}", obj.ToString());
                                                    }
                                                }
                                            }
                                        }

                                        if (eExpr.GetType() == typeof(BlockLookupParameter))
                                        {
                                            BlockLookupParameter blup = eExpr as BlockLookupParameter;
                                            if (blup != null)
                                            {
                                                BlockParameterPropertyDescriptorCollection props = blup.PropertyDescription;
                                                Array pDataTable;
                                                foreach (BlockParameterPropertyDescriptor prop in props)
                                                {
                                                    ed.WriteMessage("\n\t{0}", prop.PropertyName);
                                                    pDataTable = prop.ValueSetValues;
                                                    if (pDataTable != null)
                                                    {
                                                        foreach (object obj in pDataTable)
                                                        {
                                                            ed.WriteMessage("\n\t{0}", obj.ToString());
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                        if (eExpr.GetType() == typeof(BlockFlipParameter))
                                        {
                                            BlockFlipParameter blup = eExpr as BlockFlipParameter;
                                            if (blup != null)
                                            {
                                                BlockParameterPropertyDescriptorCollection props = blup.PropertyDescription;
                                                Array pDataTable;
                                                foreach (BlockParameterPropertyDescriptor prop in props)
                                                {
                                                    ed.WriteMessage("\n\t{0}", prop.PropertyName);
                                                    pDataTable = prop.ValueSetValues;
                                                    if (pDataTable != null)
                                                    {
                                                        foreach (object obj in pDataTable)
                                                        {
                                                            ed.WriteMessage("\n\t{0}", obj.ToString());
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                        //---------  add other actions / parameters same way here   -------------------//
                                    }
                                }
                            }
                        }
                        catch (System.Exception ex)
                        {
                            Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(ex.ToString());
                        }
                    }
                }
            }
        }

{code}

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 12 of 14
cean_au
in reply to: Hallex

ed.GetEntity("\nSelect dynamic block"

 

this still needs a dyna block first, not create one from scrath by program.

Message 13 of 14
Hallex
in reply to: cean_au

Do it yourself

I go out from there

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 14 of 14
hermann.weiss
in reply to: Hallex

Hello,
I would like to edit dynamic blocks with .Net. Unfortunately I do not find much on the internet. It seems as if Autodesk did not want to publish anything.

Can I learn more about this subject? Do you have a more recent example?
Or any other info?

Many thanks in advance

Hermann White

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost