0 Members and 1 Guest are viewing this topic.
< .. >For just finding out if a block is referenced or not, Database.Purge()should do that, but I've not tested it with dynamic blocks.
Try this very basic sample
If I unerstand right this code may helps, try itCode: [Select] public static void UpdateSwitch(Database db, BlockReference bref, string pname, string pvalue) { Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor; try { using (Transaction tr = db.TransactionManager.StartTransaction()) { TypedValue[] filterlist = new TypedValue[2]; filterlist[0] = new TypedValue(0, "INSERT"); filterlist[1] = new TypedValue(2, "`*U*,switch"); SelectionFilter filter = new SelectionFilter(filterlist); PromptSelectionResult res = ed.SelectAll(filter); if (res.Status != PromptStatus.OK) { ed.WriteMessage("\nNo \"switch\" block references selected"); return; } if (res.Value.Count != 0) { SelectionSet select = res.Value; ed.WriteMessage("\nSelected:\t{0}", select.GetObjectIds().Count()); foreach (ObjectId brId in select.GetObjectIds()) { BlockReference blkRef = (BlockReference)tr.GetObject(brId, OpenMode.ForRead); // Get the dynamic block's property collection if (blkRef.IsDynamicBlock) { BlockTableRecord btr = tr.GetObject(bref.DynamicBlockTableRecord, OpenMode.ForRead, false) as BlockTableRecord; if (btr.Name == "switch") { DynamicBlockReferencePropertyCollection pc = blkRef.DynamicBlockReferencePropertyCollection; // Loop through, getting the info for each property if (!blkRef.IsWriteEnabled) blkRef.UpgradeOpen(); foreach (DynamicBlockReferenceProperty prop in pc) { if (prop.PropertyName == pname) { prop.Value = pvalue; } } blkRef.DowngradeOpen(); }// } } } tr.Commit(); } } catch (System.Exception ex) { ed.WriteMessage(ex.Message + "\n" + ex.StackTrace); } } [CommandMethod("InsertSwitch", "swo", CommandFlags.Modal | CommandFlags.Redraw)] public static void InsertSwitchBlock() { Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; // db.ObjectAppended += new ObjectEventHandler(dbase_ObjectAppended);//removed by fail Editor ed = doc.Editor; string blkname = "switch"; BlockReference bref = null; try { using (Transaction tr = db.TransactionManager.StartTransaction()) { BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead, false); BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite, false); BlockTableRecord blk = (BlockTableRecord)tr.GetObject(bt[blkname], OpenMode.ForRead, false); Point3d ip = ed.GetPoint("\nPick insertion point: ").Value; Plane plan = new Plane(Point3d.Origin, Vector3d.ZAxis); bref = new BlockReference(ip, blk.ObjectId); bref.BlockUnit = UnitsValue.Inches; bref.Rotation = 0; bref.ScaleFactors = new Scale3d(1.0); btr.AppendEntity(bref); tr.AddNewlyCreatedDBObject(bref, true); SetAttributes(db, tr, bref); //UpdateSwitch(db, bref, "Visibility", "Disconnected"); tr.Commit(); } PromptKeywordOptions opts = new PromptKeywordOptions("Choose a Visibility? [Connected/Disconnected]", "Connected Disconnected"); opts.AllowArbitraryInput = true; PromptResult res = ed.GetKeywords(opts); ed.WriteMessage("\nChoosen\t{0}", res.StringResult); string choose = res.StringResult; UpdateSwitch(db, bref, "Visibility", choose); } catch (System.Exception ex) { ed.WriteMessage("\n{0}\n{1}", ex.Message, ex.StackTrace); } finally { // db.ObjectAppended -= new ObjectEventHandler(dbase_ObjectAppended);//removed by fail } } public static void SetAttributes(Database db, Transaction tr, BlockReference bref) { BlockTableRecord btrec = tr.GetObject(bref.BlockTableRecord, OpenMode.ForRead) as BlockTableRecord; if (btrec.HasAttributeDefinitions) { Autodesk.AutoCAD.DatabaseServices.AttributeCollection atcoll = bref.AttributeCollection; foreach (ObjectId subid in btrec) { Entity ent = (Entity)subid.GetObject(OpenMode.ForRead); AttributeDefinition attDef = ent as AttributeDefinition; if (attDef != null) { AttributeReference attRef = new AttributeReference(); attRef.SetDatabaseDefaults(); attRef.SetAttributeFromBlock(attDef, bref.BlockTransform); attRef.Position = attDef.Position.TransformBy(bref.BlockTransform); attRef.Tag = attDef.Tag; attRef.TextString = attDef.TextString; attRef.AdjustAlignment(db); atcoll.AppendAttribute(attRef); tr.AddNewlyCreatedDBObject(attRef, true); } } } }~'J'~
public static void UpdateSwitch(Database db, BlockReference bref, string pname, string pvalue) { Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor; try { using (Transaction tr = db.TransactionManager.StartTransaction()) { TypedValue[] filterlist = new TypedValue[2]; filterlist[0] = new TypedValue(0, "INSERT"); filterlist[1] = new TypedValue(2, "`*U*,switch"); SelectionFilter filter = new SelectionFilter(filterlist); PromptSelectionResult res = ed.SelectAll(filter); if (res.Status != PromptStatus.OK) { ed.WriteMessage("\nNo \"switch\" block references selected"); return; } if (res.Value.Count != 0) { SelectionSet select = res.Value; ed.WriteMessage("\nSelected:\t{0}", select.GetObjectIds().Count()); foreach (ObjectId brId in select.GetObjectIds()) { BlockReference blkRef = (BlockReference)tr.GetObject(brId, OpenMode.ForRead); // Get the dynamic block's property collection if (blkRef.IsDynamicBlock) { BlockTableRecord btr = tr.GetObject(bref.DynamicBlockTableRecord, OpenMode.ForRead, false) as BlockTableRecord; if (btr.Name == "switch") { DynamicBlockReferencePropertyCollection pc = blkRef.DynamicBlockReferencePropertyCollection; // Loop through, getting the info for each property if (!blkRef.IsWriteEnabled) blkRef.UpgradeOpen(); foreach (DynamicBlockReferenceProperty prop in pc) { if (prop.PropertyName == pname) { prop.Value = pvalue; } } blkRef.DowngradeOpen(); }// } } } tr.Commit(); } } catch (System.Exception ex) { ed.WriteMessage(ex.Message + "\n" + ex.StackTrace); } } [CommandMethod("InsertSwitch", "swo", CommandFlags.Modal | CommandFlags.Redraw)] public static void InsertSwitchBlock() { Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; // db.ObjectAppended += new ObjectEventHandler(dbase_ObjectAppended);//removed by fail Editor ed = doc.Editor; string blkname = "switch"; BlockReference bref = null; try { using (Transaction tr = db.TransactionManager.StartTransaction()) { BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead, false); BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite, false); BlockTableRecord blk = (BlockTableRecord)tr.GetObject(bt[blkname], OpenMode.ForRead, false); Point3d ip = ed.GetPoint("\nPick insertion point: ").Value; Plane plan = new Plane(Point3d.Origin, Vector3d.ZAxis); bref = new BlockReference(ip, blk.ObjectId); bref.BlockUnit = UnitsValue.Inches; bref.Rotation = 0; bref.ScaleFactors = new Scale3d(1.0); btr.AppendEntity(bref); tr.AddNewlyCreatedDBObject(bref, true); SetAttributes(db, tr, bref); //UpdateSwitch(db, bref, "Visibility", "Disconnected"); tr.Commit(); } PromptKeywordOptions opts = new PromptKeywordOptions("Choose a Visibility? [Connected/Disconnected]", "Connected Disconnected"); opts.AllowArbitraryInput = true; PromptResult res = ed.GetKeywords(opts); ed.WriteMessage("\nChoosen\t{0}", res.StringResult); string choose = res.StringResult; UpdateSwitch(db, bref, "Visibility", choose); } catch (System.Exception ex) { ed.WriteMessage("\n{0}\n{1}", ex.Message, ex.StackTrace); } finally { // db.ObjectAppended -= new ObjectEventHandler(dbase_ObjectAppended);//removed by fail } } public static void SetAttributes(Database db, Transaction tr, BlockReference bref) { BlockTableRecord btrec = tr.GetObject(bref.BlockTableRecord, OpenMode.ForRead) as BlockTableRecord; if (btrec.HasAttributeDefinitions) { Autodesk.AutoCAD.DatabaseServices.AttributeCollection atcoll = bref.AttributeCollection; foreach (ObjectId subid in btrec) { Entity ent = (Entity)subid.GetObject(OpenMode.ForRead); AttributeDefinition attDef = ent as AttributeDefinition; if (attDef != null) { AttributeReference attRef = new AttributeReference(); attRef.SetDatabaseDefaults(); attRef.SetAttributeFromBlock(attDef, bref.BlockTransform); attRef.Position = attDef.Position.TransformBy(bref.BlockTransform); attRef.Tag = attDef.Tag; attRef.TextString = attDef.TextString; attRef.AdjustAlignment(db); atcoll.AppendAttribute(attRef); tr.AddNewlyCreatedDBObject(attRef, true); } } } }