有没有办法使用C#获取autocad(.dwg)中的所有Polylines?

ton*_*919 2 c# autocad autocad-plugin

我不想在运行时选择特定的折线.有没有办法在运行时使用C#直接获取.dwg文件中的所有折线?AutoCAD有一个名为DATAEXTRACTION的命令来获取不同对象的相关信息(例如折线,圆,点等),但我不知道它是否可以在C#中调用和使用.

仅供参考:从
http://through-the-interface.typepad.com/through_the_interface/2007/04/iterating_throu.html获取运行期间特定折线的示例代码:

Transaction tr = db.TransactionManager.StartTransaction();
using (tr)
{
   DBObject obj = tr.GetObject(per.ObjectId, OpenMode.ForRead);
   Polyline lwp = obj as Polyline; // Get the selected polyline during runtime
   ...
}
Run Code Online (Sandbox Code Playgroud)

小智 8

听起来你正在寻找这样的东西.如果不需要,请删除图层条件.

public ObjectIdCollection SelectAllPolylineByLayer(string sLayer)
{
    Document oDwg = Application.DocumentManager.MdiActiveDocument; 
    Editor oEd = oDwg.Editor;

    ObjectIdCollection retVal = null;

    try {
        // Get a selection set of all possible polyline entities on the requested layer
        PromptSelectionResult oPSR = null;

        TypedValue[] tvs = new TypedValue[] {
            new TypedValue(Convert.ToInt32(DxfCode.Operator), "<and"),
            new TypedValue(Convert.ToInt32(DxfCode.LayerName), sLayer),
            new TypedValue(Convert.ToInt32(DxfCode.Operator), "<or"),
            new TypedValue(Convert.ToInt32(DxfCode.Start), "POLYLINE"),
            new TypedValue(Convert.ToInt32(DxfCode.Start), "LWPOLYLINE"),
            new TypedValue(Convert.ToInt32(DxfCode.Start), "POLYLINE2D"),
            new TypedValue(Convert.ToInt32(DxfCode.Start), "POLYLINE3d"),
            new TypedValue(Convert.ToInt32(DxfCode.Operator), "or>"),
            new TypedValue(Convert.ToInt32(DxfCode.Operator), "and>")
        };

        SelectionFilter oSf = new SelectionFilter(tvs);

        oPSR = oEd.SelectAll(oSf);

        if (oPSR.Status == PromptStatus.OK) {
            retVal = new ObjectIdCollection(oPSR.Value.GetObjectIds());
        } else {
            retVal = new ObjectIdCollection();
        }
    } catch (System.Exception ex) {
        ReportError(ex);
    }

    return retVal;
}
Run Code Online (Sandbox Code Playgroud)


Tra*_*ore 5

2015年1月12日更新

这也会起作用,让你不必处理所有类型的值...

我写了一篇关于这个主题的博客文章,请查看.

public IList<ObjectId> GetIdsByType()
{
    Func<Type,RXClass> getClass = RXObject.GetClass;

    // You can set this anywhere
    var acceptableTypes = new HashSet<RXClass>
    {
        getClass(typeof(Polyline)),
        getClass(typeof (Polyline2d)),
        getClass(typeof (Polyline3d))
    };

    var doc = Application.DocumentManager.MdiActiveDocument;
    using (var trans = doc.TransactionManager.StartOpenCloseTransaction())
    {
        var modelspace = (BlockTableRecord)
        trans.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(doc.Database), OpenMode.ForRead);

        var polylineIds = (from id in modelspace.Cast<ObjectId>()
            where acceptableTypes.Contains(id.ObjectClass)
            select id).ToList();

        trans.Commit();
        return polylineIds;
    }
}
Run Code Online (Sandbox Code Playgroud)