Message 1 of 7
Not applicable
05-07-2014
07:48 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Folks this is my first autocad development project. I am trying to retrieve all the leaders from a specific layer. This is what I have so far:
static public void ExtractObjectsFromFileByLayer(string fileName)
{
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Editor ed = doc.Editor;
string objectFilter = "LEADER";
string layerFilter = "HOMES";
// Create a database and try to load the file
Database db = new Database(false, true);
using (db)
{
try
{
db.ReadDwgFile(
fileName,
System.IO.FileShare.Read,
false,
""
);
}
catch (System.Exception)
{
ed.WriteMessage("\nUnable to read drawing file.");
return;
}
}
// Create a TypedValue array to define the filter
// by object types (dxf code 0) and layers (dxf code 8)
TypedValue[] tvs = new TypedValue[] {
new TypedValue(0, objectFilter),
new TypedValue(8, layerFilter)
};
// Assign the filter criteria to a SelectionFilter object
SelectionFilter filter = new SelectionFilter(tvs);
// Setup a Prompt Selection Result object with the SelectionFilter criteria
PromptSelectionResult res = ed.SelectAll(filter);
if (res.Status == PromptStatus.OK)
{
Transaction tr = db.TransactionManager.StartTransaction();
using (tr)
{
foreach (ObjectId objId in res.Value.GetObjectIds())
{
Entity ent =
(Entity)tr.GetObject(
objId,
OpenMode.ForRead
);
// Let's get rid of the standard namespace
const string prefix =
"Autodesk.AutoCAD.DatabaseServices.";
string typeString =
ent.GetType().ToString();
if (typeString.Contains(prefix))
typeString =
typeString.Substring(prefix.Length);
ed.WriteMessage(
"\nEntity " +
ent.ObjectId.ToString() +
" of type " +
typeString +
" found on layer " +
ent.Layer +
" with colour " +
ent.Color.ToString()
);
}
}
}
}
I am getting an error in the following line: if (res.Status == PromptStatus.OK)
If anyone can provide me some assistance it would be much appreciated.
Thanks before hand.
Solved! Go to Solution.