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

    .NET

    Reply
    Valued Contributor
    pipu123
    Posts: 57
    Registered: ‎09-27-2010
    Accepted Solution

    C# code without start external program

    1030 Views, 25 Replies
    08-24-2012 02:07 PM

    Hi everyone
    I am looking for a code.
    Without loading and unloading,without start external program (debug)  
    C# code searches active file ,for example c:\home.dwg.
    I wrote in this: netload and so on.
    It saves many hours.
    I read articles about it but I couldn't find a solution.
    I read all help about programming in c# for vs.
    Time which I have to spend waiting for starting cad every time
    takes many minutes. I wrote in vba for autocad, there is a big difference
    in waiting for a result.
    Thank You in advance
     

    Please use plain text.
    *Expert Elite*
    Posts: 6,457
    Registered: ‎06-29-2007

    Re: C# code without start external program

    08-26-2012 12:59 AM in reply to: pipu123

    Hi,

     

    can you ask again with a little bit more clearness?

    I can't interpret your question to something that I can understand, I don't see what you are searching for (as you found something that saves you hours)?

     

    If (just as one guess) you just want to have your dotNET-app automatically loaded, so you don't want to load it by hand) you might have a look to >>>this devcast<<<.

     

    - alfred -

    -------------------------------------------------------------------------
    Alfred NESWADBA
    Ingenieur Studio HOLLAUS ... www.hollaus.at
    -------------------------------------------------------------------------
    Please use plain text.
    Valued Contributor
    pipu123
    Posts: 57
    Registered: ‎09-27-2010

    Re: C# code without start external program

    08-26-2012 03:18 AM in reply to: alfred.neswadba

    I will try to explain.

    I create a new code (windows form or class)  in visual studio c#.

    Solution Explorer/ right mouse on namespace / properties

    Debug / start external program / here we choose file to autocad.

    So when I press F5 new autocad file opens, it takes about 30seconds.

    I want to write a program withou setting external program in a debug – is it possible.

     

    Instead of starting external program I would like myself open

    autocad file e:\strop.dwg

    When I press F5 C# code catches this active cad file  (strop.dwg) ,

    I wrote myself netload or put command in c#

    SendCommand("_Netload");

    I choose my dll file, I write name of command method.

    When the c# finishes, I Stop debugging and the house.dwg is still

    open. 

     

     

    I read about it on this forum , I wrote this program, please correct this

    I use visual studio 2010, autocad 2009.

     

    I read this article, it doesn't work. I missed something

     

    http://forums.autodesk.com/t5/NET/using-COM-API-in-net-dll/m-p/1334401/highlight/true#M153

     

     

    This is my code  visual 2010  c#

    Please correct and enclose  visual studio files as a solution.

     

     

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Linq;

    using System.Text;

    using System.Windows.Forms;

    using System.IO;

     

    using Autodesk.AutoCAD.ApplicationServices;

    using Autodesk.AutoCAD.Colors;

    using Autodesk.AutoCAD.ComponentModel;

    using Autodesk.AutoCAD.DatabaseServices;

    using Autodesk.AutoCAD.EditorInput;

    using Autodesk.AutoCAD.Geometry;

    using Autodesk.AutoCAD.GraphicsInterface;

    using Autodesk.AutoCAD.GraphicsSystem;

    using Autodesk.AutoCAD.LayerManager;

    using Autodesk.AutoCAD.PlottingServices;

    using Autodesk.AutoCAD.Publishing;

    using Autodesk.AutoCAD.Runtime;

    using Autodesk.AutoCAD.Windows;

     

    using Autodesk.AutoCAD.Interop;

    using Autodesk.AutoCAD.Interop.Common;

     

    using Aplikacja = Autodesk.AutoCAD.ApplicationServices.Application;

     

    namespace CadConnectwf

    {

        public partial class HitAcad : Form

        {       

            public AcadApplication cadApp;

            public AcadDocument cadDoc;

           

           

            public HitAcad()

            {

                InitializeComponent();

            }

     

            private void Form1_Load(object sender, EventArgs e)

            {

                Connect2Acad();

            }

     

     

            public bool Connect2Acad()

            {

              //  Autodesk.AutoCAD.Interop.AcadApplication _AcadApp = (AcadApplication)Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication;

              

     

            try

            {

               

                cadApp =(Autodesk.AutoCAD.Interop.AcadApplication)

                Marshal.GetActiveObject("AutoCAD.Application.17");

            }

            catch

            {

                try

                    {

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

                    cadApp.Visible = true;

                    }

                    catch (System.Exception AcadEx)

                    {

                    string sMsg = AcadEx.Message + " from: " + AcadEx.Source;

                    MessageBox.Show(sMsg, "AutoCAD_BasicCalls|Connect2Acad",

                    MessageBoxButtons.OK, MessageBoxIcon.Error);

                    return false;

                    }

            }           

                return true;

            }

     

     

        }

    }

     

     

     

     

    In help from autocad I found this but  I can’t run it I suppose it is windows form.

    Please enclose a vs studio file as a solution.

     

            [CommandMethod("ConnectToAcad")]

            public static void  ConnectToAcad()

            {

            AcadApplication acAppComObj = null;

            // "AutoCAD.Application.17" uses 2007 or 2008,

     

            const string strProgId = "AutoCAD.Application.17";

           

              // Get a running instance of AutoCAD

            try

            {

                acAppComObj = (AcadApplication)Marshal.GetActiveObject(strProgId);

            }

           catch // An error occurs if no instance is running

            {

                try

                {

                // Create a new instance of AutoCAD

                acAppComObj = (AcadApplication)Activator.CreateInstance(Type.GetTypeFromProgID(strProgId), true);

                }

     

            catch

                {

              // If an instance of AutoCAD is not created then message and exit

             

                // System.Windows.Forms.MessageBox.Show("Instance of 'AutoCAD.Application'" + " could not be created.");         

              return;

                 }

            }

     

                // Display the application and return the name and version

                acAppComObj.Visible = true;

                // System.Windows.Forms.MessageBox.Show("Now running " + acAppComObj.Name + " version " + acAppComObj.Version);

     

                // Get the active document

                AcadDocument acDocComObj;

                acDocComObj = acAppComObj.ActiveDocument;

     

                // Optionally, load your assembly and start your command or if your assembly

                // is demandloaded, simply start the command of your in-process assembly.

                acDocComObj.SendCommand("(command " + (char)34 + "NETLOAD" + (char)34 + " " + (char)34 + "g:/nauka c# cad/CsMgd1/bin/debug/CsMgd1.dll" + (char)34 + ") ");

                acDocComObj.SendCommand("MyCommand ");

                } 

    Please use plain text.
    *Expert Elite*
    Posts: 6,457
    Registered: ‎06-29-2007

    Re: C# code without start external program

    08-26-2012 03:57 AM in reply to: pipu123

    Hi,

     

    ok, three options:

     

    A) as long as you want to define own application-commands (as you use it in CommandMethod(....) ) you have to use the project-type = DLL and you have to use _NETLOAD (or the plugin-methods starting with AutoCAD 2012 or registry-settings to get the DLL automatically loaded without using command _NETLOAD shown >>>here<<<). To start debugging such a project you have two options:

    A1) as you mentioned it ... define an external application = acad.exe to be started

    A2) attach your project (to be found under menu "debug" of Visual Studio) to a running instance of acad.exe ==> that sounds close to what you are looking for. BUT: as AutoCAD can't unload managed code you have the problem that when you first start this attach to process and NETLOAD your DLL you don't get the code unloaded. So stopping debugging, rewrite the code and again attaching to acad.exe will not load the new modificated version of your DLL. So also with this method you have to restart the acad.exe

    B) your code works based on COM and works as ActiveX-Server ==> then you need again at least a starter-code that get's the connection to your external ActiveX-server. In this case I'm not sure, but I think that you are not allowed to rebuild your project as long as the DLL is currently connected via ActiveX to the acad.exe process.

    C) your code is an external EXE the connects to AutoCAD and AutoCAD plays the ActiveX-server. Exept of that you cannot define then commands as under A) you will have the option to start and stop your EXE to connect to AutoCAD and to disconnect from AutoCAD without stopping the AutoCAD-process itself. But as the EXE runs outside of the process-memory of AutoCAD that's the worst performance way to go.

     

    Hope that answers your questions, - alfred -

    -------------------------------------------------------------------------
    Alfred NESWADBA
    Ingenieur Studio HOLLAUS ... www.hollaus.at
    -------------------------------------------------------------------------
    Please use plain text.
    Valued Contributor
    pipu123
    Posts: 57
    Registered: ‎09-27-2010

    Re: C# code without start external program

    08-27-2012 11:33 AM in reply to: alfred.neswadba

    Thank You for answer
    I read almost all help/ tutorial c# for autocad.
    I am beginner with visual studio so I don't know
    is it posibble to do this.

    I will write sample code for point "A" after friday.

    I will write again my aim maybe, I will try more clearly
     step by step.

    I wrote a program in c# visual studio for autocad.
    I added reference to  AcDbMgd.dll   AcMgd.dll acax17enu.tlb.

    Debug / start external program / here we choose file to autocad.
    If it is necessery I add external program "cad.exe"

    In visual studio c# I press F5 code
    1) catches active autocad file if it is possible
    ( I wrote on hdd a file, d:\strop.dwg, I opened it before
    pressing F5 )
    or
    2) opens new autocad file
    In command line I write netload, choose dll file, write name of command
    method, it opens the file d:\strop.dwg,
    On the model space creats some drawing.
    I stopping debugging
    3) is it possible, cad file strop.dwg will be still open
     after finishing of debugging?

    Afterthat I add some lines in my c# code,I press F5
    (now I would like to save time on opening second time new autocad file
     is it possible ? or new autocad file has to be open)
    new dll will rewrite old one. In command line of autocad I write
    netload,I load dll, write command method.  

    Nowadays every time when I want to check my code new autocad file
    is opening, I stop debugging autocad is closing.
    If I want debugging often, opening and closing autocad takes about 70 seconds
    for every time.
    I would like to omit this opening and closing.
    I wonder is it possible, to save time.

    Please use plain text.
    *Expert Elite*
    Posts: 6,457
    Registered: ‎06-29-2007

    Re: C# code without start external program

    08-27-2012 12:13 PM in reply to: pipu123

    Hi,

     

    all answered with a short statement: use an 32bit os and so also a 32bit AutoCAD and you have the option "write while debugging" available in Visual Studio. As long as you work on 64bit you can just debug but not modify the code while debugging. Thats a limitation of Visual Studio.

    If you have already setup your workstation then you may use something like VmWare or any other virtual-pc alternativ so you can run a 32bit environment.

     

    All the other options will have to close and restart AutoCAD to get a modified DLL (as long as it has the same name) reloaded.

     

    - alfred -

    -------------------------------------------------------------------------
    Alfred NESWADBA
    Ingenieur Studio HOLLAUS ... www.hollaus.at
    -------------------------------------------------------------------------
    Please use plain text.
    Valued Contributor
    pipu123
    Posts: 57
    Registered: ‎09-27-2010

    Re: C# code without start external program

    08-27-2012 01:44 PM in reply to: alfred.neswadba

    Thank You very much for showing me a solution. I will read more about "write while debugging" within next weeks.

     

    Please use plain text.
    Valued Contributor
    pipu123
    Posts: 57
    Registered: ‎09-27-2010

    Re: C# code without start external program

    08-29-2012 02:43 AM in reply to: pipu123

    A  Please explain me how this works "write while debugging". Maybe send an example. I searched on internet and I couldn’t find this therm.

     

    B  So in visual studio I create code, I compile it do dll, this dll I open in autocad.

    During writing code in c# visual studio for autocad  , doesn’t have connection between them. How to debug code ?  I wrote an example.

    How people cope with checking a code( debugging) when they write huge codes in c# / vb in visual studio.

     

    I enclose vs files.

    Thank You in Advance

     

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.IO;

     

    using Autodesk.AutoCAD.ApplicationServices;

    using Autodesk.AutoCAD.Colors;

    using Autodesk.AutoCAD.ComponentModel;

    using Autodesk.AutoCAD.DatabaseServices;

    using Autodesk.AutoCAD.EditorInput;

    using Autodesk.AutoCAD.Geometry;

    using Autodesk.AutoCAD.GraphicsInterface;

    using Autodesk.AutoCAD.GraphicsSystem;

    using Autodesk.AutoCAD.LayerManager;

    using Autodesk.AutoCAD.PlottingServices;

    using Autodesk.AutoCAD.Publishing;

    using Autodesk.AutoCAD.Runtime;

    using Autodesk.AutoCAD.Windows;

    using Autodesk.AutoCAD.Interop;

     

     

    namespace _28._08._2012

    {

        public class Class1

        {

     

            [CommandMethod("CreateMText")]

            public static void CreateMText()

            {

                // Get the current document and database

                Document acDoc = Application.DocumentManager.MdiActiveDocument;

                Database acCurDb = acDoc.Database;

     

                // Start a transaction

                using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())

                {

                    // Open the Block table for read

                    BlockTable acBlkTbl;

                    acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,

                                                 OpenMode.ForRead) as BlockTable;

     

                    // Open the Block table record Model space for write

                    BlockTableRecord acBlkTblRec;

                    acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],

                                                    OpenMode.ForWrite) as BlockTableRecord;

     

                    // Create a multiline text object

                    MText acMText = new MText();

                    acMText.SetDatabaseDefaults();

                    acMText.Location = new Point3d(2, 2, 0);

                    acMText.Width = 4;

                    acMText.Contents = "This is a text string for the MText object.";

     

                    acBlkTblRec.AppendEntity(acMText);

                    acTrans.AddNewlyCreatedDBObject(acMText, true);

     

                    // Save the changes and dispose of the transaction

                    acTrans.Commit();

                    acDoc.SendStringToExecute("._zoom _all ", true, false, false);

                }

            }

           

        }

    }

    Please use plain text.
    *Expert Elite*
    dgorsman
    Posts: 3,287
    Registered: ‎10-12-2006

    Re: C# code without start external program

    08-29-2012 10:09 AM in reply to: pipu123

    Not being able to edit code on the fly while AutoCAD is open is mildly annoying, but what is, is.  I take a logical, procedural approach to development so its not really a problem.  Flailing away at code as its running instead of test/diagnose/implement/test isn't a very rigourous development environment, making it difficult to produce well organized code (at least, in my experience - *extremely* experienced coders can get away with this).

    ----------------------------------
    If you are going to fly by the seat of your pants, expect friction burns.
    Adopt. Adapt. Overcome. Or be overcome.


    Please use plain text.
    *Expert Elite*
    Posts: 6,457
    Registered: ‎06-29-2007

    Re: C# code without start external program

    08-29-2012 12:52 PM in reply to: dgorsman

    Hi,

     

    >> Not being able to edit code on the fly while AutoCAD is open is mildly annoying, but what is, is

    As mentioned, that is only valid for 64bit. If you run a 32bit os you can edit-while-debug - and of course - it's a big advantage and makes development much faster.

    And if you don't have a 32bit os then install a virtual-pc environment on your system that runs a 32bit os and AutoCAD.

     

    >> Please explain me how this works "write while debugging". Maybe send an example

    I can't send a sample for that because you can do that with every code as long as the debugged session runs in 32bit mode.

    If the problem is that you can't set the acad.exe to be started for debugging then you use the Express-release of Visual Studio. Therefor you'll find two options to get that running:

    a) check the Wizards within that ObjectARX-kit. I don't use them but I'm quite sure that using them you will have the correct settings within your project-user-file. Great source for that and around AutoCAD and dotNET is >>>here<<<.

     

    >> So in visual studio I create code, I compile it do dll, this dll I open in autocad.

    You set the options within your visual-studio project to start the acad.exe when you press "play" for start debugging your project. So Visual Studio starts AutoCAD for you (with some hooks) and when you NETLOAD your DLL every breakpoint or every exception (when enabled to stop on exception in your project) will stop at the code in the Visual Studio environment.

    For 32bit session you can now modify the code, for a 64bit session a breakpoint or exception also stops the code (before you can continue), so you have the chance for getting information about variables, stack, .... but you can't edit.

     

    HTH, - alfred -

    -------------------------------------------------------------------------
    Alfred NESWADBA
    Ingenieur Studio HOLLAUS ... www.hollaus.at
    -------------------------------------------------------------------------
    Please use plain text.