Active Contributor
Posts: 46
Registered: ‎09-04-2012
Message 1 of 6 (1,090 Views)

Drawing from an Autocad-loaded form (modal) in C# .Net

1090 Views, 5 Replies
10-18-2012 08:53 AM



What I need is: inside Autocad, the user should be able to type in a command and Windows Form should show asking for relevant information that will draw lines and text on the current drawing or a new one depending on user's choice.


I have a Visual Studio project with the 3.5 framework, Autocad plug-in template and its corresponding "myCommands.cs" file. I added a Windows Form and customized it so the user can input data for the creation of the lines to be drawn.


This is a representation of what I have on "myCommands.cs"



using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;

[assembly: CommandClass(typeof(Test_010.MyCommands))]

namespace Test_010
    public class MyCommands
        public void ElCommando()
            myFormo newFormo = myFormo();


 And for the click event of a certain (the only) button in the form I have this



private void btnCreate_Click(object sender, EventArgs e)
// The code below works to draw lines, polylines, etc in the current active document, cannot create a new one and make it the active one
//Saving info in a custom struct named myStruct in an attempt to return the struct to the //ElCommando //but no clue how that would work since Application.ShowModalDialog(newFormo); is used //instead of newFormo.Show() and even then I would not know how or where to change the //return type for the Show() event
//Cannot create a new autocad database here, I get a "File Error" COM Exception
//of course using using Autodesk.AutoCAD.ApplicationServices;
//Document NewDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.Add(@"acadiso.dwt");
//that's where I get the error
//Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument = NewDoc;
//I'd like to use that line too when the user chooses to draw in a new document myStruct newStruct; newStruct.x1 = double.Parse(textBox1.Text); newStruct.y1 = double.Parse(textBox2.Text); newStruct.x2 = double.Parse(textBox3.Text); newStruct.y2 = double.Parse(textBox4.Text); using (Transaction acTrans = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database.TransactionManager.StartTransaction()) {
// Open the Block table for read BlockTable acBlkTbl; acBlkTbl = acTrans.GetObject(Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Database.BlockTableId, OpenMode.ForRead) as BlockTable; // Open the Block table record Model space for write BlockTableRecord acBlkTblRec; acBlkTblRec = (BlockTableRecord)acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite); //Lines Line zero = new Line(new Point3d(newStruct.x1, newStruct.y1, 0), new Point3d(newStruct.x2, newStruct.y2, 0)); zero.SetDatabaseDefaults(); //zero.Linetype = Autodesk.AutoCAD.GraphicsSystem.LinePattern.Dashed.ToString();<---- Also, Can't explicitly change from that type to string? An equivalent method? zero.Color = Autodesk.AutoCAD.Colors.Color.FromRgb(0, 0, 252); // Add the new object to the block table record and the transaction acBlkTblRec.AppendEntity(zero); acTrans.AddNewlyCreatedDBObject(zero, true); // Save the new object to the database acTrans.Commit(); } this.Close(); }

 What I would like would be for the ElCommando to show the form and to somehow gather data from the modal form and then whether the user picked created new drawing, draw the objects in the current drawing or a new drawing.


Thanks in advance for your help.


In another note. How do I get to draw a dynamic number of lines?



*Expert Elite*
Posts: 2,114
Registered: ‎04-29-2006
Message 2 of 6 (1,077 Views)

Re : Drawing from an Autocad-loaded form (modal) in C# .Net

10-18-2012 09:51 AM in reply to: DouceDeux



Try to add CommandFlag.Session to the CommandMethod attribute.

Gilles Chanteau
Active Contributor
Posts: 46
Registered: ‎09-04-2012
Message 3 of 6 (1,065 Views)

I'm trying to send data back to the command function (IMP...

10-18-2012 11:49 AM in reply to: _gile

I'm trying to send data back to the command function (IMPORTANT) and create a new drawing file and make it the active document for AutoCAD (Not necessary if first case is solved).


CommandFlag.Session did not work =/


I also tried the other longer way of creating a new document in the Windows Form click event code but it doesn't work either.


string strTemplatePath = "acadiso.dwt";
DocumentCollection acDocMgr = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager;
Document acDoc = acDocMgr.Add(strTemplatePath);
acDocMgr.MdiActiveDocument = acDoc;


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

Re: I'm trying to send data back to the command function (IMP...

10-18-2012 01:19 PM in reply to: DouceDeux

If you use CommandFlags.Session you have to use LockDocument,

see an explanation about this here:


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

Re: I'm trying to send data back to the command function (IMP...

10-18-2012 03:54 PM in reply to: DouceDeux

Try this code snip on your form


        private void btnCreate_Click(object sender, EventArgs e)
            string filename = @"c:\Test\newdoc.dwg";
            double x1; double x2; double y1; double y2;

            if (!(double.TryParse(textBox1.Text, out x1) &&
             double.TryParse(textBox2.Text, out y1) &&
             double.TryParse(textBox3.Text, out x2) &&
             double.TryParse(textBox4.Text, out y2)))
                MessageBox.Show("One or more parameters doesn't entered!");
                DocumentCollection docs = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager as DocumentCollection;
                Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

                string temppath = "acadiso.dwt";

                string fullpath = HostApplicationServices.Current.FindFile(temppath, doc.Database, FindFileHint.Default);

                using (DocumentLock doclock = doc.LockDocument())
                    using (Database newdb = new Database(false, true))
                        newdb.ReadDwgFile(fullpath, System.IO.FileShare.ReadWrite,false, "");

                        using (Transaction newtr = newdb.TransactionManager.StartTransaction())

                            ObjectId mSpaceId = SymbolUtilityServices.GetBlockModelSpaceId(newdb);

                            BlockTableRecord newbtr = (BlockTableRecord)newtr.GetObject(mSpaceId, OpenMode.ForWrite);

                            Line line = new Line(new Point3d(x1, y1, 0), new Point3d(x2, y2, 0));


                            newtr.AddNewlyCreatedDBObject(line, true);


                            newdb.SaveAs(filename, false,DwgVersion.Current,newdb.SecurityParameters);

            catch (Autodesk.AutoCAD.Runtime.Exception ex)


Distinguished Contributor
Posts: 105
Registered: ‎12-29-2005
Message 6 of 6 (1,019 Views)

Re: Drawing from an Autocad-loaded form (modal) in C# .Net

10-19-2012 05:13 AM in reply to: DouceDeux


Maybe I miss read but why not just pass "this" to your newFormo  .... "myFormo newFormo = myFormo(this);"




Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
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!