Trying to read block attributes from drawing gives me eInvalidInput most of the time
I don't really understand this error
This was working fine 2 days ago and now is just failing most of the time.
Anyone could help me out. The class Atribute map is just a simple Key/Value pair class that store the block attributes.
this is the code is failing
/// <see cref="https://forums.autodesk.com/t5/net/find-block-inside-a-polyline/td-p/7586489"/>
public static IEnumerable<AttributeMap> GetBlockAttributesInsidePolyline(ObjectId id)
{
Document doc = Application.DocumentManager.MdiActiveDocument;
doc.LockDocument();
Database db = doc.Database;
Editor ed = doc.Editor;
ObjectId[] ids = null;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
try
{
Polyline pline = (Polyline)tr.GetObject(id, OpenMode.ForRead);
PromptSelectionResult selection = ed.SelectByPolyline(pline, PolygonSelectionMode.Window, new TypedValue(0, "INSERT"));
if (selection.Status == PromptStatus.OK)
{
ed.SetImpliedSelection(selection.Value);
ids = selection.Value.GetObjectIds(); ----------->here is whare it hapens most of the time---------------------------------------
}
}
catch (Autodesk.AutoCAD.Runtime.Exception e)
{
Controller.TagController.CurrentDrawing.Logger.Error(string.Format("Polyline Handle {0} had an error: {1} ",
id.Handle.ToString(), e.Message));
}
}
return GetBlockAttributes(ids);
}
/// <see cref="http://through-the-interface.typepad.com/through_the_interface/2006/09/getting_autocad.html"/>
/// <param name="ids">ObjectIds</param>
public static IEnumerable<AttributeMap> GetBlockAttributes(ObjectId[] ids)
{
//There should be just one object Id in the ids
Document doc = Application.DocumentManager.MdiActiveDocument;
doc.LockDocument();
Database db = doc.Database;
List<AttributeMap> attributes = new List<AttributeMap>();
if (ids != null)
{
using (Transaction tr = db.TransactionManager.StartOpenCloseTransaction())
{
foreach (ObjectId id in ids)
{
BlockReference blkRef = (BlockReference)tr.GetObject(id, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(blkRef.BlockTableRecord, OpenMode.ForRead);
Controller.TagController.CurrentDrawing.Logger.Info("Block: " + btr.Name);
btr.Dispose();
AttributeCollection attCol = blkRef.AttributeCollection;
foreach (ObjectId attId in attCol)
{
AttributeReference attRef = (AttributeReference)tr.GetObject(attId, OpenMode.ForRead);
string str = string.Format(" Attribute Tag: {0}{1} Attribute String: {2}", attRef.Tag, System.Environment.NewLine, attRef.TextString);
Controller.TagController.CurrentDrawing.Logger.Info(str);
attributes.Add(new AttributeMap { Field = attRef.Tag, Value = attRef.TextString });
}
}
tr.Commit();
}
}
return attributes;
}
Solved! Go to Solution.
Solved by _gile. Go to Solution.
aramosvizcarra a écrit :
this is the code is failing
/// <see cref="https://forums.autodesk.com/t5/net/find-block-inside-a-polyline/td-p/7586489"/>
public static IEnumerable<AttributeMap> GetBlockAttributesInsidePolyline(ObjectId id)
{
Document doc = Application.DocumentManager.MdiActiveDocument;
doc.LockDocument();
Database db = doc.Database;
Editor ed = doc.Editor;
ObjectId[] ids = null;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
try
{
Polyline pline = (Polyline)tr.GetObject(id, OpenMode.ForRead);
PromptSelectionResult selection = ed.SelectByPolyline(pline, PolygonSelectionMode.Window, new TypedValue(0, "INSERT"));
if (selection.Status == PromptStatus.OK)
{
ed.SetImpliedSelection(selection.Value);
ids = selection.Value.GetObjectIds(); ----------->here is whare it hapens most of the time---------------------------------------
}
}
catch (Autodesk.AutoCAD.Runtime.Exception e)
{
Controller.TagController.CurrentDrawing.Logger.Error(string.Format("Polyline Handle {0} had an error: {1} ",
id.Handle.ToString(), e.Message));
}
}return GetBlockAttributes(ids);
}
Try removing: ed.SetImpliedSelection(selection.Value);
This expression was only a way to display the result of the selection in the example.
Hi,
in which line does the error accure?
Regards Jürgen
Thank once again,that seems to solved it.
Just for curiosity why that line may be causing the issue?.
I couldn't find in the reference document that method.
aramosvizcarra a écrit :
Thank once again,that seems to solved it.
Just for curiosity why that line may be causing the issue?.
I couldn't find in the reference document that method.
After calling Editor.SetImpliedSelection, the selection set entities are highlighted/gripped, in other words, the selection set is "opened" for adding or removing entities to it. That's why you cannot acces to the SelectionSet.GetObjectIds() method.
Can't find what you're looking for? Ask the community or share your knowledge.