Hi,
I've written a program with c#.net for selecting objects and creating block without openning a window in autocad. But when I run its dll in autocad, it makes a block including many of the selected objects by me/user in one block(Please see attachments)
Please help me to solve this problem,
Thank you
Try this code this isn't mine but seems to do your work
#region "Placing block correctly on UCS" [CommandMethod("BlockKeepObjects", CommandFlags.UsePickSet)] public void BlockKeepObjectsCommand() { BlockCommand(true); } [CommandMethod("BlockReplaceObjects", CommandFlags.UsePickSet)] public void BlockReplaceObjectsCommand() { BlockCommand(false); } void BlockCommand(bool keepObjects) { // written by Thoersten Kaefer Database db = acadApp.DocumentManager.MdiActiveDocument.Database; Editor ed = acadApp.DocumentManager.MdiActiveDocument.Editor; PromptSelectionResult psr = ed.GetSelection(); if (psr.Status != PromptStatus.OK) return; PromptStringOptions pso = new PromptStringOptions( "Enter Block Name: ") { AllowSpaces = true }; PromptResult pr = ed.GetString(pso); if (pr.Status != PromptStatus.OK) return; using (Transaction tr = db.TransactionManager.StartTransaction()) { BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead); bool nameOk = false; try { SymbolUtilityServices.ValidateSymbolName(pr.StringResult, false); if (bt.Has(pr.StringResult)) ed.WriteMessage("\nBlock \"{0}\" already exists. ", pr.StringResult); else nameOk = true; } catch { ed.WriteMessage("\nInvalid block name: \"{0}\". ", pr.StringResult); } if (!nameOk) return; PromptPointOptions ppo = new PromptPointOptions( "\nPick Insertion Point"); PromptPointResult ppr = ed.GetPoint(ppo); if (ppr.Status != PromptStatus.OK) return; // Create BlockTableRecord Matrix3d ucs2wcs = ed.CurrentUserCoordinateSystem; Matrix3d wcs2ucs = ucs2wcs.Inverse(); using (BlockTableRecord btr = new BlockTableRecord() { Origin = ppr.Value, Name = pr.StringResult, Annotative = AnnotativeStates.True }) { // Add BlockTableRecord to Database bt.UpgradeOpen(); bt.Add(btr); tr.AddNewlyCreatedDBObject(btr, true); // Populate newly created BlockTableRecord with selected objects ObjectIdCollection oidc = new ObjectIdCollection(psr.Value.GetObjectIds()); if (keepObjects) db.DeepCloneObjects(oidc, btr.ObjectId, new IdMapping(), false); else btr.AssumeOwnershipOf(oidc); // Transform objects now in block from WCS to UCS // and collect attributes System.Collections.Generic.List<AttributeDefinition> attdefs = new System.Collections.Generic.List<AttributeDefinition>(); foreach (ObjectId oid in btr) { Entity ent = (Entity)tr.GetObject(oid, OpenMode.ForWrite); ent.TransformBy(wcs2ucs); AttributeDefinition ad = ent as AttributeDefinition; if (ad != null && !ad.Constant) attdefs.Add(ad); } // Create BlockReference using (BlockReference br = new BlockReference(Point3d.Origin, bt[pr.StringResult])) { br.TransformBy(ucs2wcs * Matrix3d.Displacement(ppr.Value.GetAsVector())); var occ = db.ObjectContextManager.GetContextCollection("ACDB_ANNOTATIONSCALES"); if (occ.HasContext(db.Cannoscale.Name)) Autodesk.AutoCAD.Internal.ObjectContexts.AddContext( br, occ.GetContext(db.Cannoscale.Name)); // Add BlockReference to current space BlockTableRecord cspace = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite); cspace.AppendEntity(br); tr.AddNewlyCreatedDBObject(br, true); // Add attributes to BlockReference, if any foreach (AttributeDefinition ad in attdefs) using (AttributeReference attref = new AttributeReference()) { attref.SetAttributeFromBlock(ad, br.BlockTransform); if (ad.IsMTextAttributeDefinition) attref.UpdateMTextAttribute(); attref.AdjustAlignment(db); br.AttributeCollection.AppendAttribute(attref); tr.AddNewlyCreatedDBObject(attref, true); } } } tr.Commit(); } } #endregion
~'J'~
Thank you so much,
But I run this program and was confronted with a run time error in line* :
var occ = db.ObjectContextManager.GetContextCollection("ACDB_ANNOTATIONSCALES");
if (occ.HasContext(db.Cannoscale.Name))
* Autodesk.AutoCAD.Internal.ObjectContexts.AddContext(br, occ.GetContext(db.Cannoscale.Name));
What should I do?
Thanks
What is your Acad release (version) ?
Try this line instead:
ObjectContextCollection occ = db.ObjectContextManager.GetContextCollection("ACDB_ANNOTATIONSCALES");
My Acad version is 2012.
My problem hasn't been solved. In line:
ObjectContextCollection occ = db.ObjectContextManager.GetContextCollection("ACDB_ANNOTATIONSCALES");
"ACDB_ANNOTATIONSCALES" is a collection name, but I don't know where that collection has been defined and where it has gotten a value.
In run time, occ is null.
Please let me know how can I solve this problem?
Thank you
Sorry I just have A2010, pehaps somebody else from this NG
will be help for you,
~'J'~