• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    .NET

    Reply
    Contributor
    Posts: 12
    Registered: ‎03-01-2012

    Select a Text from AutoCAD and to Display in C# windows forms

    598 Views, 8 Replies
    03-02-2012 01:21 AM

    Dear All,

    Season greetings to all experts,

     

    with respect to above subject,

     

    using System.Runtime.InteropServices;

    using Autodesk.AutoCAD.Interop;

     

    privatevoid button2_Click(object sender, EventArgse)

    {

    AcadApplication AcadApp = new Autodesk.AutoCAD.Interop.AcadApplication();

     

    AcadDocument doc1 = AcadApp.Documents.Open("c:\\test.dwg");

    AcadApp.Visible =true;

    AcadApp.ZoomExtents();

    double[] startPoint = newdouble[3]; 

    double[] endPoint = newdouble[3];

    startPoint[0] = 0; startPoint[1] = 0; startPoint[2] = 0;

    endPoint[0] = 20; endPoint[1] = 20; endPoint[2] = 0;

     

    AcadSelectionSet ssetObj = AcadApp.ActiveDocument.SelectionSets.Add("ss1");

     

    ssetObj.Select(Autodesk.AutoCAD.Interop.Common.

    AcSelect.acSelectionSetCrossingPolygon,startPoint,endPoint,Type.Missing, Type.Missing);

     

    }

     

    the idea is to select the AutoCAD text by crossingPolygon and then to display the selected text in the C# windows form label1.text.

     

    Could you please help for this above request.

     

    Thanks in Advance.

     

    regards,

    Micky.

     

    Please use plain text.
    ADN Support Specialist
    Balaji_Ram
    Posts: 345
    Registered: ‎03-21-2011

    Re: Select a Text from AutoCAD and to Display in C# windows forms

    03-02-2012 03:13 AM in reply to: Micky_1

    Hi Micky,

     

    Greetings to you too :smileyhappy:

     

    Will a pure .Net API route suit your needs ? You will not need the COM API for this :

     

    Here is a sample code :

     

                Document doc = Application.DocumentManager.MdiActiveDocument;
                Database db = doc.Database;
                Editor ed = doc.Editor;
    
                PromptSelectionOptions pso = new PromptSelectionOptions();
                pso.MessageForAdding = "\nSelect the entities";
                pso.SingleOnly = false;
    
                PromptSelectionResult psr = ed.GetSelection(pso);
    
                if (psr.Status != PromptStatus.OK) return;
                SelectionSet ss = psr.Value;
    
                foreach (SelectedObject selectedObj in ss)
                {
                    switch (selectedObj.SelectionMethod)
                    {
                        case SelectionMethod.Crossing:
                        {
                            CrossingOrWindowSelectedObject crossingWindSelectedObj = selectedObj as CrossingOrWindowSelectedObject;
                            break;
                        }
                    }
                }

     The SelectionMethod enum has other values for Window selection and so on. You can check it out yourself.

     

     



    Balaji
    Developer Technical Services
    Autodesk Developer Network

    Please use plain text.
    *Expert Elite*
    Posts: 679
    Registered: ‎04-27-2009

    Re: Select a Text from AutoCAD and to Display in C# windows forms

    03-02-2012 06:55 AM in reply to: Balaji_Ram

    The OP is obviously doing a stand-alone EXE app that automate AutoCAD (or the OP would not have to have code "new AcadApplication();" , thus cannot use .NET API.

     

    To OP:

     

    In your Select() method, you only pass in 2 points, which cannot form a polygon, thus, the first argument of the Select() method should be acSelectionSetFence.

     

    Since your selecting target is text, it would be better to supply a selection filter (the last 2 arguments of the Select() method).

     

    Anyway, assume the Select() method does return a set of AcadEnity, you simply loop through the SelectionSet to retrieve test string, like this:

     

    StringBuillder txt=new StringBuilder();

     

    foreach (object ent in ssetObj)

    {

        AcadText t=ent as AcadText

        if (T!=null)

        {

            txt.Append(t.TextString + "\n");

        }

    }

     

    //Show the text clollected in message box, or in your form.

    MessageBox.Show(t.ToString());

     

    Of course, if your selection target alos include MText, then you need to deal with it accordingly.

     

     

     

    Please use plain text.
    Contributor
    Posts: 12
    Registered: ‎03-01-2012

    Re: Select a Text from AutoCAD and to Display in C# windows forms

    03-02-2012 07:06 AM in reply to: Balaji_Ram

    Dear Mr.Balaji,

     

    Season Greetings,

     

    Thanks for your quick response,

     

    I am a beginer for .Net C#, that's why I am struggling a bit.

     

    In my codings I have some error in crossingpolygon selection method, rest everything working well.

     

    I am in need of 2 things,

     

    1. select a Text from AutoCAD by crossingpolygon method,

     

    2. store it in a string varible to display in C# windows forms.

     

    Could you please help me for this above request,

     

    I am trying hard to learn this .Net, I have a big hope that as soon as possible i will be getting a clear understanding of .net.

     

    Thanks & regards,

    Micky.

    Please use plain text.
    *Expert Elite*
    Hallex
    Posts: 1,334
    Registered: ‎10-08-2008

    Re: Select a Text from AutoCAD and to Display in C# windows forms

    03-02-2012 07:16 AM in reply to: Micky_1

    If you're using Interop better yet to define variables explicitly

    before you will be pulling them to the method / function

    From other hand you confused the selection mode

    try this code snip instead and tell us about result after :smileyhappy:

    {code} 

    object miss = Type.Missing;
    AcadSelectionSet ssetObj = AcadApp.ActiveDocument.SelectionSets.Add("ss1");
    Autodesk.AutoCAD.Interop.Common.AcSelect mode = AcSelect.acSelectionSetCrossing;
    ssetObj.Select(mode, startPoint, endPoint, miss, miss);
    MessageBox.Show(ssetObj.Count.ToString());{code}

     

    ~'J'~

    _____________________________________
    C6309D9E0751D165D0934D0621DFF27919
    Please use plain text.
    Contributor
    Posts: 12
    Registered: ‎03-01-2012

    Re: Select a Text from AutoCAD and to Display in C# windows forms

    03-02-2012 07:17 AM in reply to: norman.yuan

    Dear Mr. Norman,

     

    Season Greetings,

     

    thanks for your response,

     

    for crossingpolygon selection method I using bottom left corner and top right corner only,

     

    Could you please see my reply post to Mr. Balaji, that's my problem and I got struck.

     

    Thanks & regards,

    Micky.

    Please use plain text.
    *Expert Elite*
    Posts: 679
    Registered: ‎04-27-2009

    Re: Select a Text from AutoCAD and to Display in C# windows forms

    03-02-2012 07:42 AM in reply to: Micky_1

    OK, simply call

     

    ssetobj.Select(acSelectionSetWindow, startPoint, endPoint,....)

    Please use plain text.
    *Expert Elite*
    chiefbraincloud
    Posts: 736
    Registered: ‎02-13-2008

    Re: Select a Text from AutoCAD and to Display in C# windows forms

    03-02-2012 10:24 AM in reply to: norman.yuan

    or rather acSelectionSetCrossing, but not acSelectionSetCrossingPolygon.

    Dave O.                                                                                Sig-Logos32.png
    Please use plain text.
    Contributor
    Posts: 12
    Registered: ‎03-01-2012

    Re: Select a Text from AutoCAD and to Display in C# windows forms

    03-02-2012 07:08 PM in reply to: Hallex

    Dear Mr. Hallex,

     

    Thanks very much for your kindness & support, the code is working well.

     

    where I will get reference or help file for the all the methods?

     

    for examples :

     

    ssetObj.Select(mode, startPoint, endPoint, miss, miss);

     

    [object filter Type = Type.missing] --- where to find examples or help files?

     

    [object filter Data = Type.missing]--- where to find examples or help files?

     

    for examples :

    AcadDocument doc1 = acApp.Documents.Open(textBox1.Text);

    In open method,

    I have given only the file name, if I wnat to give other two inputs then how?

     

    so where I will get reference or help files for all the methods.

     

    best regards,

    Micky.

     

     

     

     

     

    Please use plain text.