无法使用C#在autocad中的给定图形中执行Select对象?

时间:2014-12-01 09:27:24

标签: c# autocad

我需要打开给定的路径图形文件,然后选择图形的所有线条,然后列出线条的编号。我不能做这些事情,因为我打开然后设置MdiActiveDocument方法返回。下面的行不会被执行。

    private static int GetNumberofLines(string drawingName)
    {
        int lineCount = 0;
        DocumentCollection docMgr = AutoCAD.ApplicationServices.Application.DocumentManager;
        Document doc = docMgr.Open(drawingName, false);

        docMgr.MdiActiveDocument = doc;        // in this line method is skipped

        TypedValue[] filterlist = new TypedValue[1]; //cursor didn't come this line..

        filterlist[0] = new TypedValue((int)DxfCode.Start, "Line");

        SelectionFilter filter = new SelectionFilter(filterlist);

        PromptSelectionOptions opts = new PromptSelectionOptions();

        opts.MessageForAdding = "Select entities to get line counts: ";

        PromptSelectionResult prmptSelRslt = doc.Editor.SelectAll(filter);

        if (prmptSelRslt.Status != PromptStatus.OK)
        {
            return 0;
        }
        else
        {
            lineCount = prmptSelRslt.Value.Count;
        }

        return lineCount;

    }

请有人告诉我如何打开和列出行号计数。

提前致谢....

2 个答案:

答案 0 :(得分:1)

有更简单的方法来获取所需的信息。我写了一篇关于这个主题的blog post。你应该看看。

    [CommandMethod("getLines")]
    public void OpenDrawing()
    {
        OpenDrawingGetLines(@"C:\saved\linetest.dwg");
    }

    public void OpenDrawingGetLines(string path)
    {
        var editor = Application.DocumentManager.MdiActiveDocument.Editor;
        if (!File.Exists(path))
        {
            editor.WriteMessage(string.Format(@"{0} doesn't exist.", path));
            return;
        }

        // wrap in using statement to open and close.
        var doc = Application.DocumentManager.Open(path, false);

        string docName = string.Empty;
        IEnumerable<ObjectId> lineIds;

        // lock the doc
        using (doc.LockDocument())

        // start transaction
        using (var trans = doc.TransactionManager.StartOpenCloseTransaction())
        {
            // get the modelspace
            var modelspace = (BlockTableRecord)
               trans.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(doc.Database), OpenMode.ForRead);

            // get the lines ObjectIds
            lineIds = from id in modelspace.Cast<ObjectId>()
                      where id.ObjectClass.IsDerivedFrom(RXObject.GetClass(typeof(Line)))
                      select id;

            docName = doc.Name;
            trans.Commit();
        }

        var message = string.Format("{0} Lines in {1}", lineIds.Count(), docName);
        editor.WriteMessage(message);
        Application.ShowAlertDialog(message);
    }

答案 1 :(得分:0)

您应该使用DocumentCollectionExtension。 (在VB中)

Dim strFileName As String = "C:\campus.dwg"

Dim acDocMgr As DocumentCollection = Application.DocumentManager

If (File.Exists(strFileName)) Then
    DocumentCollectionExtension.Open(acDocMgr, strFileName, False)
Else
    acDocMgr.MdiActiveDocument.Editor.WriteMessage("File " & strFileName & _
                                                   " does not exist.")
End If
相关问题