.NET

Reply
Active Member
Madmat67250
Posts: 6
Registered: ‎06-05-2012
Message 1 of 12 (507 Views)

Autocad to Excel with events

507 Views, 11 Replies
11-22-2012 11:14 PM

Hello,

 

First, sorry for my English...

 

I'm searching for help according to the Events Handling in vb.net

 

I'm trying to extract infromations from Autocad (this part of the code is OK), and to insert it in an Excel Cell chosen by the user.

Basicly that's not a problem, using the Getobject method, but in this case I want to use the "click" event to allow the user to tell the program in which cell of the active Excel Workbook the values have to be inserted.

I can't get the Event Handling to work (the dll is started from autocad via netload, and should take control of the excel application).

 

Is anybody familiar with these events? And can may help me ;-)

Which references have to be loaded in Visual Studio in order to get all this working?

 

Matthieu

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 2 of 12 (477 Views)

Re: Autocad to Excel with events

11-23-2012 09:20 AM in reply to: Madmat67250

I think you may use Clipboard to make this work

Open Excel book and Acad document

then run this code in AutoCAD, after the any text

was selected, then its content is already in clipboard,

so you could to switch to excel, select your cell and

paste the text by clicking "Paste" from right button click

That's all

 

#region "System Imports"
using System;
using System.Text;
using System.Windows.Forms;

#endregion

#region "AutoCAD Imports"
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
#endregion


[assembly: CommandClass(typeof(MyProject.MyClass))]

namespace MyProject
{
    public class MyClass
    {

        public string cellText ="";

        
         void ed_SelectionAdded(object sender, SelectionAddedEventArgs e)
        {
            
            ObjectId id = ((SelectionSet)e.AddedObjects).GetObjectIds()[0];
            if (id.ObjectClass.IsDerivedFrom(RXClass.GetClass(typeof(DBText))))
            {
                using (Transaction tr = ((Editor)sender).Document.TransactionManager.StartTransaction())
                {
                    DBText txt = tr.GetObject(id,OpenMode.ForRead, false) as DBText;
               
                    if (txt == null)
                    {
                        MessageBox.Show("NULL");
                        return;
                    }
                  
                    System.Windows.Forms.Clipboard.Clear();
                    cellText = txt.TextString;
                    System.Windows.Forms.Clipboard.SetText(txt.TextString, TextDataFormat.Text);
                }
            }
        }

        [CommandMethod("ClipText","ctx", CommandFlags.Modal)]
        public void testCopyClipBoard()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

            Editor ed = doc.Editor;
            try{
            ed.SelectionAdded += new SelectionAddedEventHandler(ed_SelectionAdded); 
 
            PromptSelectionOptions pso = new PromptSelectionOptions();

            pso.MessageForRemoval="\nMust be select a single text.";

            pso.MessageForAdding = "\nSelect a single text: ";

            pso.AllowDuplicates = false;


            pso.SinglePickInSpace = true;

            PromptSelectionResult pres = ed.GetSelection(pso);
        
            if (pres.Status != PromptStatus.OK)

                return ;

            SelectionSet set = pres.Value;

            ObjectId[] ids = set.GetObjectIds();

            if (ids.Length != 1)

                return ;
            }
            catch (System.Exception ex)
            {

                ed.WriteMessage(ex.Message + "\n" + ex.StackTrace +"\n");
            }
            finally
            {
                ed.WriteMessage("\n" + cellText + "\n");
            }
        }

}

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Active Member
Madmat67250
Posts: 6
Registered: ‎06-05-2012
Message 3 of 12 (466 Views)

Re: Autocad to Excel with events

11-23-2012 10:07 AM in reply to: Hallex

Thanx for answer, but that's not what I want to do.

 

I really want to :

 

1-clic a region in Autocad

2-my program will extract its centroid (X coord and Y coord)

3-make the excel application active

4-make my activeWorkbook active

5-Wait for me to choose a first cell (event handling in the active excel application)

6-Paste X Coord

7-Wait for me to choose a second cell (event handling in the active excel application)

8-Paste Y Coord

9-End of program

 

At this time I'm at point 4, and everything is working fine, but I don't know how to use events handling to watch excel application, waiting for my cell selection...

I could first select my cell in Excel and easily use ExcelApp.ActiveRange.value="myXCoord"..., but that's not what I'm searching for.

 

 

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 4 of 12 (461 Views)

Re: Autocad to Excel with events

11-23-2012 10:37 AM in reply to: Madmat67250

first I need to know what the language you prefer?

VB.Net or C#?

and also, do you want to use Excel.Interop library or late-binding

to work with them

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Active Member
Madmat67250
Posts: 6
Registered: ‎06-05-2012
Message 5 of 12 (454 Views)

Re: Autocad to Excel with events

11-23-2012 11:02 AM in reply to: Hallex

I definitly only know the vb.net language, and about using  Excel.Interop library or late-binding... I don't really know...

Is there an advantage using the one or the other?

 

Is this late-binding : "xlApp = CreateObject("Excel.Application")"  ???

If yes, that's common for me to use (I used to write VBA applications), but I'll try the other way if that's better.

 

By the way, in our office we are using excel 2010 and on some older PCs excel 2003.

Will one of these 2 solutions allow the code to run with both versions?

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 6 of 12 (451 Views)

Re: Autocad to Excel with events

11-23-2012 11:52 AM in reply to: Madmat67250

I think the late binding is enough to work with both Excel releases

but to write the events will be very difficult for me

Well, will be back in tomorrow,

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Active Member
Madmat67250
Posts: 6
Registered: ‎06-05-2012
Message 7 of 12 (428 Views)

Re: Autocad to Excel with events

11-24-2012 01:43 AM in reply to: Hallex

If it's easier for you to write in C#, I'll convert it with an online converter, like this one : http://www.developerfusion.com/tools/convert/csharp-to-vb/

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 8 of 12 (417 Views)

Re: Autocad to Excel with events

11-24-2012 07:09 AM in reply to: Madmat67250

Well, try it:

https://dl.dropbox.com/u/18024145/AcadExcelTools.zip

paste to cell is handled event twice, so ignore the second one

and press Cancel to ignore

Tell me when you've copy the code, I will delete it

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Active Member
Madmat67250
Posts: 6
Registered: ‎06-05-2012
Message 9 of 12 (384 Views)

Re: Autocad to Excel with events

11-25-2012 10:15 PM in reply to: Hallex

I've downloaded your file. I'll try it in the morning.

You can delete it.

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

Re: Autocad to Excel with events

11-25-2012 10:23 PM in reply to: Madmat67250

OK,

Don't forget to change on your settings

before test

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.