[CommandMethod("VDBlkAttr")]
public static void VisibilityDynamicBlockAttribute()
{
Document doc = null;
Database db = null;
Editor ed = null;
string blkName = "Block1";
string visPropName = "VISIBILITY";
string visPropValue = "VisVal1";
try
{
doc = AcadApp.DocumentManager.MdiActiveDocument;
if (doc == null)
throw new System.Exception("No MdiActiveDocument");
db = doc.Database;
ed = doc.Editor;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
var bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
if (!bt.Has(blkName))
throw new System.Exception("Block not found: " + blkName);
var blkId = bt[blkName];
var ms = (BlockTableRecord)tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForWrite);
var insPnt = new Point3d(10, 20, 0);
var blkRef = new BlockReference(insPnt, blkId);
ms.AppendEntity(blkRef);
tr.AddNewlyCreatedDBObject(blkRef, true);
//Add Attributes to BlockReference, this is a separate action in C#
BlockTableRecord blk = (BlockTableRecord)tr.GetObject(blkRef.BlockTableRecord, OpenMode.ForRead);
// if (blkRef.IsDynamicBlock)
// blk = (BlockTableRecord)tr.GetObject(blkRef.BlockTableRecord, OpenMode.ForRead);
//else
// blk = (BlockTableRecord)tr.GetObject(blkRef.DynamicBlockTableRecord, OpenMode.ForRead);
foreach (ObjectId id in blk)
{
DBObject dbObj = (DBObject)tr.GetObject(id, OpenMode.ForRead);
AttributeDefinition attDef = dbObj as AttributeDefinition;
if ((attDef != null) && (!attDef.Constant))
{
using (AttributeReference attRef = new AttributeReference())
{
attRef.SetAttributeFromBlock(attDef, blkRef.BlockTransform);
attRef.TextString = "";
blkRef.AttributeCollection.AppendAttribute(attRef);
tr.AddNewlyCreatedDBObject(attRef, true);
}
}
}
if (blkRef.IsDynamicBlock) //comment this when it's not a Dynamic block
{
var dynProps = blkRef.DynamicBlockReferencePropertyCollection;
foreach (DynamicBlockReferenceProperty dynProp in dynProps)
{
if (!dynProp.ReadOnly && Regex.IsMatch(dynProp.PropertyName, visPropName, RegexOptions.IgnoreCase))
{
var allVals=dynProp.GetAllowedValues();
var dynVals=new Dictionary<string,string>(StringComparer.OrdinalIgnoreCase);
allVals.Cast<string>().ToList().ForEach(n=> dynVals[n]=n);
if (!dynVals.ContainsKey(visPropValue))
ed.WriteMessage("\n Warning: {0} Key not found: {1}. Apply Default.", visPropName, visPropValue);
else
dynProp.Value = dynVals[visPropValue];
}
}
}
tr.Commit();
}
}
catch (System.Exception ex)
{
if (ed != null)
ed.WriteMessage("\n Error in VisibilityDynamicBlockAttribute: {0}", ex.Message);
}
}
Hope you found useful information here.
Post your messages or any doubts in the comment section below...!!
No comments:
Post a Comment