.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Autocad to Excel with events

11 REPLIES 11
Reply
Message 1 of 12
Madmat67250
1213 Views, 11 Replies

Autocad to Excel with events

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

11 REPLIES 11
Message 2 of 12
Hallex
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
Message 3 of 12
Madmat67250
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.

 

 

Message 4 of 12
Hallex
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
Message 5 of 12
Madmat67250
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?

Message 6 of 12
Hallex
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
Message 7 of 12
Madmat67250
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/

Message 8 of 12
Hallex
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
Message 9 of 12
Madmat67250
in reply to: Hallex

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

You can delete it.

Message 10 of 12
Hallex
in reply to: Madmat67250

OK,

Don't forget to change on your settings

before test

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 11 of 12
Madmat67250
in reply to: Hallex

Thank you very much for your help.

I changed a few lines so that the program makes exactly what I want him to do...

Thanks’ to you I've now understood how to use event handler between 2 programs.

Everything is now working fine.

Message 12 of 12
Hallex
in reply to: Madmat67250

Glad I could help

Would not to be this accepted as solution maybe...

Keep coding

 

Cheers  Smiley Happy

_____________________________________
C6309D9E0751D165D0934D0621DFF27919

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost