Message 1 of 11
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I want insert a name of block but it is error all times. Can anybody help me?
using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.Runtime; using Autodesk.AutoCAD.Geometry; using Autodesk.AutoCAD.Internal; namespace insertBlock { class BlockJig : EntityJig { Point3d mCenterPt, mActualPoint; public BlockJig(BlockReference br) : base(br) { mCenterPt = br.Position; } protected override SamplerStatus Sampler(JigPrompts prompts) { JigPromptPointOptions jigOpts = new JigPromptPointOptions(); jigOpts.UserInputControls = (UserInputControls.Accept3dCoordinates | UserInputControls.NoZeroResponseAccepted | UserInputControls.NoNegativeResponseAccepted); jigOpts.Message = "\nEnter insert point: "; PromptPointResult dres = prompts.AcquirePoint(jigOpts); if (mActualPoint == dres.Value) { return SamplerStatus.NoChange; } else { mActualPoint = dres.Value; } return SamplerStatus.OK; } protected override bool Update() { mCenterPt = mActualPoint; try { ((BlockReference)Entity).Position = mCenterPt; } catch (System.Exception) { return false; } return true; } public Entity GetEntity() { return Entity; } } public class Commands { [CommandMethod("BJIG")] public void CreateBlockWithJig() { string fileName = "C:\\Users\\bedyr\\Desktop\\sembol\\sembol.dwg"; Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; // First let's get the name of the block PromptStringOptions opts = new PromptStringOptions("\nEnter block name: "); PromptResult pr = ed.GetString(opts); if (pr.Status == PromptStatus.OK) { using (Transaction tr = doc.TransactionManager.StartTransaction()) { BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead); if (!bt.Has(pr.StringResult)) { Database tmpDb = new Database(false, true); tmpDb.ReadDwgFile(fileName, System.IO.FileShare.Read, true, ""); // add the block to the ActiveDrawing blockTable db.Insert("pr.StringResult", tmpDb, true); ObjectId blockId = bt[pr.StringResult]; // We loop until the jig is cancelled while (pr.Status == PromptStatus.OK) { // Create the block reference and // add it to the jig Point3d pt = new Point3d(0, 0, 0); BlockReference br = new BlockReference(pt, blockId); // Start annot-scale support code BlockTableRecord bd = (BlockTableRecord)tr.GetObject(blockId, OpenMode.ForRead); // Using will dispose of the block definition // when no longer needed using (bd) { if (bd.Annotative == AnnotativeStates.True) { ObjectContextManager ocm = db.ObjectContextManager; ObjectContextCollection occ = ocm.GetContextCollection("ACDB_ANNOTATIONSCALES"); ObjectContexts.AddContext(br, occ.CurrentContext); } } // End annot-scale support code BlockJig entJig = new BlockJig(br); // Perform the jig operation pr = ed.Drag(entJig); if (pr.Status == PromptStatus.OK) { // If all is OK, let's go and add the // entity to the modelspace BlockTableRecord ms = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite); ms.AppendEntity(entJig.GetEntity()); tr.AddNewlyCreatedDBObject(entJig.GetEntity(), true); // Start attribute support code bd = (BlockTableRecord)tr.GetObject(blockId, OpenMode.ForRead); // Add the attributes foreach (ObjectId attId in bd) { Entity ent = (Entity)tr.GetObject(attId, OpenMode.ForRead); if (ent is AttributeDefinition) { AttributeDefinition ad = (AttributeDefinition)ent; AttributeReference ar = new AttributeReference(); ar.SetAttributeFromBlock(ad, br.BlockTransform); br.AttributeCollection.AppendAttribute(ar); tr.AddNewlyCreatedDBObject(ar, true); } } // End attribute support code // Call a function to make the graphics display // (otherwise it will only do so when we Commit) doc.TransactionManager.QueueForGraphicsFlush(); } } } tr.Commit(); } } } } }
Solved! Go to Solution.