Autocad .NET get attributes of selected blocks - c#

I am new to autocad scripting and am trying to create an application which allows the user to select blocks and show the associated attributes of the block. I have found an alternative solution for now but would like to try an understand why my initial method was not working.
What I initially tried to do is create a selection set from the user input, get the objectid and try to create a blocktablerecord from this information. However every time I tried it returned Null.
here is the code I was using:
public void getattrib()
{
Database acCurDb;
acCurDb = Application.DocumentManager.MdiActiveDocument.Database;
Document acDoc = Application.DocumentManager.MdiActiveDocument;
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Open the Block table for read
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
PromptSelectionResult acSSPrompt = acDoc.Editor.GetSelection();
if (acSSPrompt.Status == PromptStatus.OK)
{
SelectionSet acSSet = acSSPrompt.Value;
ObjectId anothertest = acSSet[0].ObjectId;
BlockTableRecord br = acTrans.GetObject(anothertest, OpenMode.ForRead) as BlockTableRecord;

If You select the block, it's BlockReference not BlockTableRecord.
BlockTableRecord is a block definition.
If You want BlockTableRecord you can read it's ObjectId as property BlockTableRecord of selected BlockReference

Related

AutoCAD 2021 .NET API: Getting all blockreferences in a database?

just trying to find an efficient way of retrieving blockreferences from a drawing's database so I can explode them. Our shop's CNC mill isn't capable of reading blocks, only geometric entities. Our current export function creates a new drawing for each selected block, recreates it there, and then tries to explode it so the mill can properly read its files - right now we have to explode each new drawing manually, but doing so does achieve the results we want.
Tyler
The following code should explode all first generation block references in model space. Note: It does not explode sub entity block references.
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.ApplicationServices.Core;
using Autodesk.AutoCAD.Runtime;
public static class Commands
{
[CommandMethod("ExplodeAll")]
public static void ExplodeAll()
{
try
{
Database database = Application.DocumentManager.MdiActiveDocument.Database;
using Transaction transaction = database.TransactionManager.StartTransaction();
using BlockTable blockTable = transaction.GetObject(database.BlockTableId, OpenMode.ForRead) as BlockTable;
ObjectId modelId = blockTable[BlockTableRecord.ModelSpace];
using BlockTableRecord model = transaction.GetObject(modelId, OpenMode.ForRead) as BlockTableRecord;
RXClass rxBlockReference = RXClass.GetClass(typeof(BlockReference));
foreach (ObjectId id in model)
{
if (id.IsNull || id.IsErased || id.IsEffectivelyErased || !id.IsValid)
continue;
if (id.ObjectClass == rxBlockReference)
{
using BlockReference reference = transaction.GetObject(id, OpenMode.ForRead) as BlockReference;
reference.ExplodeToOwnerSpace();
reference.Erase();
}
}
transaction.Commit();
}
catch { }
}
}

Adding elements to model space the correct way using .NET API

Method One
_AcDb.Line oLine = new _AcDb.Line(ptStart, ptEnd);
AddToModelSpace("PLOT", oLine);
Where AddToModelSpace is:
public static void AddToModelSpace(string strLayer, _AcDb.Entity oEntity)
{
_AcAp.Document acDoc = _AcAp.Application.DocumentManager.MdiActiveDocument;
_AcDb.Database acCurDb = acDoc.Database;
_AcEd.Editor ed = acDoc.Editor;
using (_AcDb.BlockTable bt = acCurDb.BlockTableId.GetObject(_AcDb.OpenMode.ForRead) as _AcDb.BlockTable)
using (_AcDb.BlockTableRecord ms = bt[_AcDb.BlockTableRecord.ModelSpace].GetObject(_AcDb.OpenMode.ForWrite) as _AcDb.BlockTableRecord)
ms.AppendEntity(oEntity);
oEntity.Layer = strLayer;
oEntity.Dispose();
}
Method Two
// Get the current document and database
_AcAp.Document docActive = _AcAp.Application.DocumentManager.MdiActiveDocument;
_AcDb.Database docDB = docActive.Database;
// Start a transaction
using (_AcDb.Transaction acTrans = docDB.TransactionManager.StartTransaction())
{
// Open the Block table for read
_AcDb.BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(docDB.BlockTableId,
_AcDb.OpenMode.ForRead) as _AcDb.BlockTable;
// Open the Block table record Model space for write
_AcDb.BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[_AcDb.BlockTableRecord.ModelSpace],
_AcDb.OpenMode.ForWrite) as _AcDb.BlockTableRecord;
// Create line
using (_AcDb.Line acLine = new _AcDb.Line(ptStart, ptEnd))
{
// Add the new object to the block table record and the transaction
acBlkTblRec.AppendEntity(acLine);
acTrans.AddNewlyCreatedDBObject(acLine, true);
}
// Save the new object to the database
acTrans.Commit();
}
I have used AddToModelSpace in my project so I hope it is fine!
Method Two is the way Autodesk recommends in the developer's documentation (you can read this section).
In Method One, you use the ObjectId.GetObject() method to open the BlockTable and the model space 'BlockTableRecord'. This method uses the top transaction to open object which means that there's an active transaction you should use to add the newly created entity. You can get it with Database.TransactionManager.TopTransaction. If you don't want to use a transaction at all, you have to use the "for advanced use only" ObjectId.Open() method.
A Method Three should be using some extension methods to be called from within a transaction. Here's a simplified (non error checking) extract of the ones I use.
static class ExtensionMethods
{
public static T GetObject<T>(
this ObjectId id,
OpenMode mode = OpenMode.ForRead,
bool openErased = false,
bool forceOpenOnLockedLayer = false)
where T : DBObject
{
return (T)id.GetObject(mode, openErased, forceOpenOnLockedLayer);
}
public static BlockTableRecord GetModelSpace(this Database db, OpenMode mode = OpenMode.ForRead)
{
return SymbolUtilityServices.GetBlockModelSpaceId(db).GetObject<BlockTableRecord>(mode);
}
public static ObjectId Add (this BlockTableRecord owner, Entity entity)
{
var tr = owner.Database.TransactionManager.TopTransaction;
var id = owner.AppendEntity(entity);
tr.AddNewlyCreatedDBObject(entity, true);
return id;
}
}
Using example:
using (var tr = db.TransactionManager.StartTransaction())
{
var line = new Line(startPt, endPt) { Layer = layerName };
db.GetModelSpace(OpenMode.ForWrite).Add(line);
tr.Commit();
}

How to insert an entity in a specific layout in Autocad using .net?

I am trying to insert a blockReference (in my case a legend) into a specific layout amongst other layouts.
Here it's the code that I used:
BlockTable blockTable = transaction.GetObject(database.BlockTableId, OpenMode.ForWrite) as BlockTable;
BlockTableRecord blkTbRecPaper = transaction.GetObject(blockTable[BlockTableRecord.PaperSpace], OpenMode.ForWrite) as BlockTableRecord;
blkTbRecPaper.AppendEntity(blockReference);
My problem is that it only works when the last selected layout is the one that I am looking for. Is there a way to directly specify the wanted layout instead addin the blockReference into the blockTableREcord of the paper space ?
You need to open the layout dictionary in order to get all layouts on the drawing. Then you can open the respective BlockTableRecord and insert the new block reference.
using (Transaction tr = db.TransactionManager.StartTransaction())
{
DBDictionary layoutDic
= tr.GetObject(
db.LayoutDictionaryId,
OpenMode.ForRead,
false
) as DBDictionary;
foreach (DBDictionaryEntry entry in layoutDic)
{
ObjectId layoutId = entry.Value;
Layout layout
= tr.GetObject(
layoutId,
OpenMode.ForRead
) as Layout;
ed.WriteMessage(
String.Format(
"{0}--> {1}",
Environment.NewLine,
layout.LayoutName
)
);
}
tr.Commit();
}
See a better sample at http://adndevblog.typepad.com/autocad/2012/05/listing-the-layout-names.html

Is it possible to edit block attributes in AutoCAD using Autodesk.AutoCAD.Interop?

I have developed an external WPF application to generate drawings in c#. I have been able to draw, dimension, add blocks and every thing else required by the application using Autodesk.AutoCAD.Interop, however I can't seem to populate The title block, or generate a parts list.
All the examples I've seen are based on the mechanism that requires the application to run as a plugin inside AutoCAD. The truth is, inserting a line used is one or two lines of code using ModelSpace.InsertLine, now, it's at least 8 lines of code!
Is there a way to achieve this functionality using the Autodesk.AutoCAD.Interop? Or is there a way to combine using the interop with a plugin that can be called from the external exe?
Any pointers on this will be appreciated.
Thanks.
EDIT
To illustrate:
// before - Draw Line with Autodesk.AutoCAD.Interop
private static AcadLine DrawLine(double[] startPoint, double[] endPoint)
{
AcadLine line = ThisDrawing.ModelSpace.AddLine(startPoint, endPoint);
return line;
}
// Now - Draw line with Autodesk.AutoCAD.Runtime
[CommandMethod("DrawLine")]
public static Line DrawLine(Coordinate start, Coordinate end)
{
// Get the current document and database
// Get the current document and database
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
// Start a transaction
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Open the Block table for read
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
// Open the Block table record Model space for write
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
// Create a line that starts at 5,5 and ends at 12,3
Line acLine = new Line(start.Point3d, end.Point3d);
acLine.SetDatabaseDefaults();
// Add the new object to the block table record and the transaction
acBlkTblRec.AppendEntity(acLine);
acTrans.AddNewlyCreatedDBObject(acLine, true);
// Save the new object to the database
acTrans.Commit();
return acLine;
}
}
Yes, you can absolutely combine the two approaches.
Write an in-process DLL that does the work in AutoCAD. Make the commands you wish to call available to the command line by flagging your public methods with [CommandMethod("MethodName")].
Get AutoCAD started or connected via interop calls.
Using the interop AcadApplication, netload your DLL, and then call your work functions from the command line.
*Bonus * You can pass interop parameters to internal commands much easier this way too.
Here's an example of how you could build a commandmethod in-process and then call it via COM:
[CommandMethod("EditBlockAtt")]
public void EditBlockAtt()
{
var acDb = HostApplicationServices.WorkingDatabase;
var acEd = AcadApplication.DocumentManager.MdiActiveDocument.Editor;
var blockNamePrompt = acEd.GetString(Environment.NewLine + "Enter block name: ");
if (blockNamePrompt.Status != PromptStatus.OK) return;
var blockName = blockNamePrompt.StringResult;
var attNamePrompt = acEd.GetString(Environment.NewLine + "Enter attribute name: ");
if (attNamePrompt.Status != PromptStatus.OK) return;
var attName = attNamePrompt.StringResult;
var acPo = new PromptStringOptions(Environment.NewLine + "Enter new attribute value: "){ AllowSpaces = true };
var newValuePrompt = acEd.GetString(acPo);
if (newValuePrompt.Status != PromptStatus.OK) return;
var newValue = newValuePrompt.StringResult;
using (var acTrans = acDb.TransactionManager.StartTransaction())
{
var acBlockTable = acTrans.GetObject(acDb.BlockTableId, OpenMode.ForRead) as BlockTable;
if (acBlockTable == null) return;
var acBlockTableRecord = acTrans.GetObject(acBlockTable[BlockTableRecord.ModelSpace], OpenMode.ForRead) as BlockTableRecord;
if (acBlockTableRecord == null) return;
foreach (var blkId in acBlockTableRecord)
{
var acBlock = acTrans.GetObject(blkId, OpenMode.ForRead) as BlockReference;
if (acBlock == null) continue;
if (!acBlock.Name.Equals(blockName, StringComparison.CurrentCultureIgnoreCase)) continue;
foreach (ObjectId attId in acBlock.AttributeCollection)
{
var acAtt = acTrans.GetObject(attId, OpenMode.ForRead) as AttributeReference;
if (acAtt == null) continue;
if (!acAtt.Tag.Equals(attName, StringComparison.CurrentCultureIgnoreCase)) continue;
acAtt.UpgradeOpen();
acAtt.TextString = newValue;
}
}
acTrans.Commit();
}
}
Then from an interop AcadApplication, netload the dll and call the method from the commandline in this format:
(Command "EditBlockAtt" "BlockName" "AttributeName" "NewValue")
However if you want to go pure Interop, this may get you what you need given you have an AcadDocument object at runtime:
foreach (AcadEntity ent in acadDoc.ModelSpace)
{
var block = ent as AcadBlockReference;
if (block == null) continue;
{
if (!block.Name.Equals("BlockName", StringComparison.CurrentCultureIgnoreCase)) continue;
var atts = block.GetAttributes() as object[];
if (atts == null) continue;
foreach (var attribute in atts.OfType<AcadAttributeReference>()
.Where(attribute => attribute.TagString.Equals("AttributeName",
StringComparison.CurrentCultureIgnoreCase)))
{
attribute.TextString = "New Value";
}
}
}
Also note this is using the AutoCAD 2012 Interop libraries. YMMV.

How to get Only one acadobject by selection set

I have some trouble to select the targeted acadObject. I get the input via selectionset.SelectonScreen method.
Here i can get more number of object from modelspace based on my filter condition.But i need only one object from the user.
Here i mentioned my code below:
AcadSelectionSet selset= null;
selset=currDoc.selectionset.add("Selset");
short[] ftype=new short[1];
object[] fdata=new object[1];
ftype[0]=2;//for select the blockreference
fdata[0]=blkname;
selset.selectOnScreen ftype,fdata; // Here i can select any no. of blocks according to filter value but i need only one block reference.
Please help me to solve this problem.
That's possible using other Autocad .NET libraries (instead of Interop library). But fortunately, one does not exclude the other.
You will need to reference the libraries containing the following namespaces:
using Autodesk.Autocad.ApplicationServices
using Autodesk.Autocad.EditorInput
using Autodesk.Autocad.DatabaseServices
(you get those downloading the Object Arx libraries for free from Autodesk):
You will need to access the Editor from an autocad Document.
By the code you've shown, you're probably working with AcadDocument documents.
So, to transform an AcadDocument into a Document, do this:
//These are extension methods and must be in a static class
//Will only work if Doc is saved at least once (has full name) - if document is new, the name will be
public static Document GetAsAppServicesDoc(this IAcadDocument Doc)
{
return Application.DocumentManager.OfType<Document>().First(D => D.Name == Doc.FullOrNewName());
}
public static string FullOrNewName(this IAcadDocument Doc)
{
if (Doc.FullName == "")
return Doc.Name;
else
return Doc.FullName;
}
Once you've got a Document, get the Editor, and the GetSelection(Options, Filter)
The Options contains a property SingleOnly and a SinglePickInSpace. Setting that to true does what you want. (Try both to see wich works better)
//Seleciton options, with single selection
PromptSelectionOptions Options = new PromptSelectionOptions();
Options.SingleOnly = true;
Options.SinglePickInSpace = true;
//This is the filter for blockreferences
SelectionFilter Filter = new SelectionFilter(new TypedValue[] { new TypedValue(0, "INSERT") });
//calls the user selection
PromptSelectionResult Selection = Document.Editor.GetSelection(Options, Filter);
if (Selection.Status == PromptStatus.OK)
{
using (Transaction Trans = Document.Database.TransactionManager.StartTransaction())
{
//This line returns the selected items
AcadBlockReference SelectedRef = (AcadBlockReference)(Trans.GetObject(Selection.Value.OfType<SelectedObject>().First().ObjectId, OpenMode.ForRead).AcadObject);
}
}
this is a direct quote from autocad developer help
http://docs.autodesk.com/ACD/2013/ENU/index.html?url=files/GUID-CBECEDCF-3B4E-4DF3-99A0-47103D10DADD.htm,topicNumber=d30e724932
There is tons of documentation the AutoCAD .NET API.
you will need to have
[assembly: CommandClass(typeof(namespace.class))]
above your namespace if you want to be able to invoke this command from the command line after you NetLoad the .dll, if it is a classLibrary.
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
[CommandMethod("SelectObjectsOnscreen")]
public static void SelectObjectsOnscreen()
{
// Get the current document and database
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
// Start a transaction
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
// Request for objects to be selected in the drawing area
PromptSelectionResult acSSPrompt = acDoc.Editor.GetSelection();
// If the prompt status is OK, objects were selected
if (acSSPrompt.Status == PromptStatus.OK)
{
SelectionSet acSSet = acSSPrompt.Value;
// Step through the objects in the selection set
foreach (SelectedObject acSSObj in acSSet)
{
// Check to make sure a valid SelectedObject object was returned
if (acSSObj != null)
{
// Open the selected object for write
Entity acEnt = acTrans.GetObject(acSSObj.ObjectId,
OpenMode.ForWrite) as Entity;
if (acEnt != null)
{
// Change the object's color to Green
acEnt.ColorIndex = 3;
}
}
}
// Save the new object to the database
acTrans.Commit();
}
// Dispose of the transaction
}
}

Categories