Valued Contributor
Posts: 67
Registered: ‎02-03-2012
Message 1 of 4 (300 Views)

Draw lines from WinForm.

300 Views, 3 Replies
03-18-2012 02:19 AM


I wrote some stand+alone application (WinForm=, where user can specify dimensions.


Now after button click I want to switch form to current AutoCAD file and prompt user to specify point.

From this point I want to start drawing few lines.


And I don't know how to switch to AutoCad and get current drawing space.


Can anyone help me? Any articles about this would be nice for me :smileyhappy:

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

Re: Draw lines from WinForm.

03-18-2012 06:51 AM in reply to: GrzesiekGP

Take look at this thread





Valued Contributor
Posts: 67
Registered: ‎02-03-2012
Message 3 of 4 (280 Views)

Re: Draw lines from WinForm.

03-18-2012 11:11 AM in reply to: GrzesiekGP

Thanks, I've made something that works:


object obj = System.Runtime.InteropServices.Marshal.GetActiveObject("AutoCAD.Application");
AcadApplication acad = (AcadApplication)obj as AcadApplication;
acad.WindowState = AcWindowState.acMax;
AcadDocument adoc = (AcadDocument)acad.ActiveDocument as AcadDocument;
AcadUtility acutil = (AcadUtility)adoc.Utility as AcadUtility;
string prompt1 = null;
double[] pnt;
prompt1 = "Enter block insert point: ";
pnt = acutil.GetPoint(Type.Missing, prompt1);

 But tell me one more thing please.

I have also written code, which works good from AutoCAD application like:


using acadApp = Autodesk.AutoCAD.ApplicationServices.Application;
Editor ed = acadApp.DocumentManager.MdiActiveDocument.Editor;
Database db = HostApplicationServices.WorkingDatabase;

 using (Transaction trans = db.TransactionManager.StartTransaction())


  Matrix3d ucs = ed.CurrentUserCoordinateSystem;
                Vector3d normal = new Vector3d(0, 0, 1);
                normal = normal.TransformBy(ucs);

                Point3d finalRodStart = new Point3d(newStartX, newStartY, 0).TransformBy(ucs);
                Point3d finalRodEnd = new Point3d(newEndX, newEndY, 0).TransformBy(ucs);

                Line Rod = new Line(finalRodStart, finalRodEnd);

                BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
                BlockTableRecord btr = (BlockTableRecord)trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);

                // get current layer
                ObjectId currentLayer = db.Clayer;
                // get layer to draw rod
                ObjectId drawingLayer = layers.SetDrawingLayer(trans, db, layersEnum.layer.K_ZBR).Id;
                Rod.LayerId = (drawingLayer == ObjectId.Null) ? db.Clayer : drawingLayer;

                // if set, go back to the previous layer
                if (!Properties.Settings.Default.RememberPreviousLayer)
                    db.Clayer = Rod.LayerId;

                trans.AddNewlyCreatedDBObject(Rod, true);



 Am I able to use this code from WinForm level?

Is there any way to cast running (or start acad) application within Autodesk.AutoCAD.ApplicationServices.Application class?


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

Re: Draw lines from WinForm.

03-18-2012 10:28 PM in reply to: GrzesiekGP

THere are few ways how to do it

In example buiild your project as Class Library

Then add Form

Then add singleton to initialize form, i.e.


using System;
using System.Collections.Generic;
using System.Text;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using System.Windows.Forms;

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

namespace MyProject

    public class InitForm : IExtensionApplication

        public void Initialize()
            Form1 myForm = new Form1();
        public void Terminate()

 Then in Debug tab check radio button "Start project"





You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

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 to get help installing your software.

Ask the Community