.NET

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

Draw lines from WinForm.

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

Hello!


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*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 2 of 4 (302 Views)

Re: Draw lines from WinForm.

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

Take look at this thread

http://forums.autodesk.com/t5/NET/How-to-pass-parameter-in-interface-to-the-programe-of-dll-file/td-...

 

~'J'~

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Valued Contributor
GrzesiekGP
Posts: 67
Registered: ‎02-03-2012
Message 3 of 4 (292 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);
MessageBox.Show(pnt[0].ToString());

 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;
                }

                btr.AppendEntity(Rod);
                trans.AddNewlyCreatedDBObject(Rod, true);
                trans.Commit();

}

 

 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*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 4 of 4 (272 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();
            myForm.Show();
        }
        public void Terminate()
        {
        }
    }
}

 Then in Debug tab check radio button "Start project"

 hth

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Need installation help?

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