.NET

Reply
Contributor
Micky_1
Posts: 12
Registered: ‎03-01-2012
Message 1 of 9 (1,251 Views)

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

1251 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.

 

ADN Support Specialist
Balaji_Ram
Posts: 733
Registered: ‎03-21-2011
Message 2 of 9 (1,228 Views)

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

*Expert Elite*
norman.yuan
Posts: 1,045
Registered: ‎04-27-2009
Message 3 of 9 (1,204 Views)

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.

 

 

 

Contributor
Micky_1
Posts: 12
Registered: ‎03-01-2012
Message 4 of 9 (1,203 Views)

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.

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 5 of 9 (1,199 Views)

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
Contributor
Micky_1
Posts: 12
Registered: ‎03-01-2012
Message 6 of 9 (1,194 Views)

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.

*Expert Elite*
norman.yuan
Posts: 1,045
Registered: ‎04-27-2009
Message 7 of 9 (1,189 Views)

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,....)

*Expert Elite*
chiefbraincloud
Posts: 753
Registered: ‎02-13-2008
Message 8 of 9 (1,179 Views)

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
Contributor
Micky_1
Posts: 12
Registered: ‎03-01-2012
Message 9 of 9 (1,163 Views)

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.

 

 

 

 

 

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are You Going To Be @ AU 2014? Feel free to drop by our AU topic post and share your plans, plug a class that you're teaching, or simply check out who else from the community might be in attendance. Ohh and don't forgot to stop by the Autodesk Help | Learn | Collaborate booths in the Exhibit Hall and meet our community team if you get a chance!