Sivaperuman
Sivaperuman

Reputation: 239

Unable to perform Select object in given drawing in autocad using C#?

I need to open the given path drawing file and then select the All lines of the drawing and then list number of the lines. I cannot do these things because while i open and then set the MdiActiveDocument method is returned. Below lines are not executed.

    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;

    }

Please can anybody tell me how to open and list line number count.

Thanks in Advance....

Upvotes: 0

Views: 514

Answers (2)

Trae Moore
Trae Moore

Reputation: 1787

There are much easier ways to get the information that you need. I wrote a blog post about this subject. You should take a look.

    [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);
    }

Upvotes: 1

Alain
Alain

Reputation: 404

You Should Use the DocumentCollectionExtension. (In 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

Upvotes: 0

Related Questions