using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System.Collections.Generic;
namespace DynamicBlocks2
{
public class Commands
{
[CommandMethod("ADBP")]
static public void ApplyDynamicBlockProps()
{
Document doc =
Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
// Select the source and target block references
PromptEntityOptions peo =
new PromptEntityOptions(
"\nSelect source dynamic block reference: "
);
peo.SetRejectMessage("\nEntity is not a block.");
peo.AddAllowedClass(typeof(BlockReference), false);
PromptEntityResult per =
ed.GetEntity(peo);
if (per.Status != PromptStatus.OK)
return;
ObjectId sourceId = per.ObjectId;
peo.Message =
"\nSelect target dynamic block reference: ";
per = ed.GetEntity(peo);
if (per.Status != PromptStatus.OK)
return;
ObjectId targetId = per.ObjectId;
Transaction tr =
db.TransactionManager.StartTransaction();
using (tr)
{
// Open our block references: the source (br1)
// for read and the target (br2) for write
BlockReference br1 =
(BlockReference)tr.GetObject(
sourceId,
OpenMode.ForRead
);
BlockReference br2 =
(BlockReference)tr.GetObject(
targetId,
OpenMode.ForWrite
);
// They must both be dynamic block references
if (br1 != null && br1.IsDynamicBlock &&
br2 != null && br2.IsDynamicBlock)
{
if (br1.DynamicBlockTableRecord ==
br2.DynamicBlockTableRecord)
{
// They use the same block definition - let's assume
// the properties are in the same order
DynamicBlockReferencePropertyCollection pc1 =
br1.DynamicBlockReferencePropertyCollection;
DynamicBlockReferencePropertyCollection pc2 =
br2.DynamicBlockReferencePropertyCollection;
if (pc1.Count == pc2.Count)
{
for (int i = 0; i < pc1.Count; i++)
{
// Get each property. If they have the same
// name and are not read-only, attempt to
// copy the value from the source (prop1)
// to the target (prop2)
DynamicBlockReferenceProperty prop1 = pc1,
prop2 = pc2;
if (prop1.PropertyName == prop2.PropertyName &&
!prop1.ReadOnly && !prop2.ReadOnly)
{
prop2.Value = prop1.Value;
}
}
}
}
else
{
// If the block references refer to different
// dynamic block definitions, let's collect the
// properties for the first in a dictionary and
// attempt to apply them to the second
DynamicBlockReferencePropertyCollection pc1 =
br1.DynamicBlockReferencePropertyCollection;
// Create and populate our dictionary
Dictionary<string, DynamicBlockReferenceProperty> dict =
new
Dictionary<string, DynamicBlockReferenceProperty>();
foreach (DynamicBlockReferenceProperty prop in pc1)
{
if (!prop.ReadOnly &&
!dict.ContainsKey(prop.PropertyName))
dict.Add(prop.PropertyName, prop);
}
// Try to copy them to the target block reference's
// dynamic properties
DynamicBlockReferencePropertyCollection pc2 =
br2.DynamicBlockReferencePropertyCollection;
foreach (DynamicBlockReferenceProperty prop2 in pc2)
{
if (!prop2.ReadOnly &&
dict.ContainsKey(prop2.PropertyName))
{
try
{
DynamicBlockReferenceProperty prop1;
if (dict.TryGetValue(
prop2.PropertyName, out prop1
)
)
{
if (prop2.PropertyTypeCode ==
prop1.PropertyTypeCode)
{
prop2.Value = prop1.Value;
}
}
}
// Expand if you want to diagnose specific issues
catch { }
}
}
}
}
else
{
ed.WriteMessage(
"\nYou need to select two dynamic bock references."
);
}
tr.Commit();
}
}
}
}