How to convert Plugin to Windows Form

How to convert Plugin to Windows Form

gary_kohout
Contributor Contributor
839 Views
9 Replies
Message 1 of 10

How to convert Plugin to Windows Form

gary_kohout
Contributor
Contributor

Hello, kind of a newbie here.  I have an old VB6 program that we use to fill out title blocks and print drawings etc. that I'm converting to C#.  All of the current examples that I can find are plug ins to type commands on the command line.  What I want is a windows form where I can click buttons on the form to interact with AutoCAD.  I have played with the plug in code and it works, but how can I convert it to a windows form rather than a program?  Are there some samples out there showing this using "ObjectARX"?  All of the samples using a Windows Form that I have found are old where they only have one using statement for AutoCAD "using AutoCAD;"

0 Likes
Accepted solutions (2)
840 Views
9 Replies
Replies (9)
Message 2 of 10

Alfred.NESWADBA
Consultant
Consultant

Hi,

 

you might start here:

https://through-the-interface.typepad.com/through_the_interface/2015/09/creating-an-autocad-palette-...

 

This shows a user-control used inside an AutoCAD palette.

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
ISH-Solutions GmbH / Ingenieur Studio HOLLAUS
www.ish-solutions.at ... blog.ish-solutions.at ... LinkedIn ... CDay 2026
------------------------------------------------------------------------------------

(not an Autodesk consultant)
0 Likes
Message 3 of 10

_gile
Consultant
Consultant

Hi,

You can have a look at this little tutorial.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 4 of 10

gary_kohout
Contributor
Contributor

Thanks guys, still can't get it going.  I'm sure it' a very simple issue I'm missing.  Here's the code I have right now.  Just trying to get to the items in the "Block table" so I can get to the title blocks in model space.  Can't figure out how to grab the active document that I have opened in AutoCAD.  Keeps telling me it detected an invalid program...

garykohout_0-1641310620777.png

 

 

 

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using AcApp = Autodesk.AutoCAD.ApplicationServices.Application;

namespace ACADTest2
{
    public partial class Form1 : Form
    {
     
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Document acDoc;
            acDoc = AcApp.DocumentManager.MdiActiveDocument;
            Database db = HostApplicationServices.WorkingDatabase;
            Transaction trans = db.TransactionManager.StartTransaction();

            BlockTable blkTbl = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
            BlockTableRecord msBlkRec = trans.GetObject(blkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

        }
    }
}

 

 

 

0 Likes
Message 5 of 10

_gile
Consultant
Consultant
Accepted solution

Hi,

Your issue does not seem to be related to the Windows Form API.

Did you start from a working AutoCAD plug-in with a simple CommandMethod?



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 6 of 10

gary_kohout
Contributor
Contributor

Yes, I started with some training I found online.  This code works.  When I run it, AutoCAD starts, then I type "netload" then have to select a dll name "AU.dll".  The I type in "myfirstline" and it draws the line.

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;

namespace AU.MySoftware
{
    public class Initialization : IExtensionApplication
    {
        #region Commands

        [CommandMethod("MyFirstLine")]
        public void cmdMyFirst()
        {
            Database db = HostApplicationServices.WorkingDatabase;
            Transaction trans = db.TransactionManager.StartTransaction();

            BlockTable blkTbl = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
            BlockTableRecord msBlkRec = trans.GetObject(blkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

            Point3d pnt1 = new Point3d(0, 0, 0);
            Point3d pnt2 = new Point3d(10, 10, 0);

            Line lineObj = new Line(pnt1, pnt2);
            msBlkRec.AppendEntity(lineObj);
            trans.AddNewlyCreatedDBObject(lineObj, true);
            trans.Commit();
        }

        #endregion


        #region Initialization

        void IExtensionApplication.Initialize()
        {

        }

        void IExtensionApplication.Terminate()
        {

        }
        #endregion

    }
}

 

0 Likes
Message 7 of 10

_gile
Consultant
Consultant

So, then you can add a new item to your project (a Windows Form) and make interaction within the dialog  box (Windows form) and the command class (AutoCAD plugin as shown here.

 

PS: you should not have CommandMethod in a class implementing IExtensionApplication overall if the Initialize and Terminate methods are empty.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 8 of 10

gary_kohout
Contributor
Contributor

But isn't this still an AutoCAD plug in?  When I originally converted the VB6 code to C#, the only using statement I had for AutoCAD was "using AutoCAD;"  and I was then able to run my program and it would find the AutoCAD app that was running and I was getting info from the document I had opened.  I'm trying to upgrade to the newer method, but maybe it's only for plug ins?

0 Likes
Message 9 of 10

_gile
Consultant
Consultant
Accepted solution

The AutoCAD .NET API only runs "in-process" (i.e. a netloaded plugin).
Read this topic.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 10 of 10

gary_kohout
Contributor
Contributor

Thanks!  That clears up a lot of my confusion.  Really appreciate your help!