Is it possible to run a custom command at Start Tab?

Is it possible to run a custom command at Start Tab?

JBerns
Advisor Advisor
1,872 Views
8 Replies
Message 1 of 9

Is it possible to run a custom command at Start Tab?

JBerns
Advisor
Advisor

Greetings, .NET Customization Community.

 

Is it possible to run a custom .NET command at the AutoCAD Start Tab? In other words, zero-document mode. I know I cannot run LISP or VBA.

 

I have developed a custom command to open drawings. It is currently written in LISP, so it works well when a drawing is open. I would also like to offer the command when all drawings are closed.

2019-01-16_9-44-02.png

I do not want to run a macro when AutoCAD starts-up. AutoCAD is already running with all drawings closed and the Start Tab is active.

 

If .NET commands are not supported in zero-document mode, then perhaps I need a standalone desktop app that would offer a similar drawing name input interface, then it passes the name of the drawing to the AutoCAD application to open. That is beyond my skill set at this time. Option to open drawing as read-write or read-only would be a bonus.

 

The work-around for now is to keep at least one drawing open to use the LISP version.

 

Thanks for your time and attention. I look forward to the replies.

 

 

Regards,

Jerry

-----------------------------------------------------------------------------------------
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
0 Likes
Accepted solutions (1)
1,873 Views
8 Replies
Replies (8)
Message 2 of 9

_gile
Consultant
Consultant

Hi,

 

You can run .NET code in zero document state, but you cannot run a command (neither a built-in nor a custom).

 

Here's a little example:

 

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;

using AcAp = Autodesk.AutoCAD.ApplicationServices.Core.Application;

namespace ZeroDocumentStateSample
{
    public class Initialization : IExtensionApplication
    {
        static DocumentCollection docMgr = AcAp.DocumentManager;

        public void Initialize()
        {
            docMgr.DocumentDestroyed += Docs_DocumentDestroyed;
        }

        public void Terminate()
        { }

        private void Docs_DocumentDestroyed(object sender, DocumentDestroyedEventArgs e)
        {
            if (docMgr.MdiActiveDocument == null)
                AcAp.ShowAlertDialog("Zero document state");
        }
    }
}


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 9

norman.yuan
Mentor
Mentor
Accepted solution

It depends on what do you mean by "custom command".

 

If it means exactly a class method decorated with CommandMethodAttribute, then, no, you cannot run it without active document, just as you cannot run other AutoCAD command. 

 

If you mean to run some code from a custom document tab (since Acad2015, we can add custom document tab into AutoCAD, just like he AutoCAD's Start tab, which was originally New tab in acad2015) to do something, such as opening new or existing drawing, as you described, then yes, you can.

 

The picture I attached shows a custom tab I created in our office to connect AutoCAD with our document storage system (ProjectWise), so user can use it like Windows Explorer built into AutoCAD, and check in/out drawing files and open/close in AutoCAD.

 

Custom Tab.png

 

 

 

Norman Yuan

Drive CAD With Code

EESignature

Message 4 of 9

JBerns
Advisor
Advisor

@_gile,

 

Thanks for the reply and info. In the LISP code, I am using vla-open and vla-activate, but no AutoCAD commands, such as OPEN.

 

Thanks for the code example, but I have a lot to learn about .NET.  I am not sure where to open or paste your code to run it.  I have much to learn to advance from LISP to .NET.

 

Thanks again.

 

Regards,

Jerry

-----------------------------------------------------------------------------------------
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
0 Likes
Message 5 of 9

_gile
Consultant
Consultant

Unlike AutoLISP which is an embedded language (with a LISP interpreter in AutoCAD), a .NET application must be compiled before it can be loaded into AutoCAD.

If you want to start with .NET for AutoCAD, you can start by reading the documentation and .NET Training Labs on this page.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 6 of 9

JBerns
Advisor
Advisor

@norman.yuan,

 

That is great news. Yes, the command would be to open drawings.

That is an impressive amount of customization you have for AutoCAD! I hope to achieve something that will require a little less sophistication.

 

The folder structure would be based on a Job Number. So drawing 123456HD1.dwg would be saved to:

   P:\Jobs\VOL_123\123400-99\123456\123456HD1.dwg

 

It would be a great convenience to the designer if he/she could run a custom command that would:

  • open a dialog box
  • accept user input of a job (drawing) name
  • parse the name to determine the path
  • confirm folder/file existence
  • confirm file availability (we're not using a document manager at this time for AutoCAD)
  • if open by someone else, prompt designer to open drawing read-only or cancel the open
  • else if drawing not open, then open drawing

The enhanced drawing Open command, currently working in LISP, is a great time saver. I hope I can rewrite this to extend its functionality to the zero-document mode (all drawings closed and Start Tab active). This would save a designer so much time from navigating the folder structure.

 

I know I have a lot to learn to develop something like this, but it's good to know it is possible.

I'll check the AU Online classes, but are there other sites or resources you can recommend for a .NET beginner?

Thanks, again!

 

 

Kind regards,

Jerry

 

-----------------------------------------------------------------------------------------
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
0 Likes
Message 7 of 9

norman.yuan
Mentor
Mentor

@Anonymous has already provided links for you to get started with AutoCAD .NET API programming. Just remind you, you need to have basic, but fairly good Microsoft .NET framework programming knowledge to start with. Furthermore, if you do intend to do something with AutoCAD's custom document tab, then you need to be able to program UI with WPF (not traditional Windows Form), which is another added technical complexity.

 

Not meant to discourage you to jump into AutoCAD .NET programming. As matter of fact, once you get into it, you probably never want to look back at VBA at all, and much less LISP (I almost forgot how to do LISP since moved to AutoCAD .NET more than 10 years ago).

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 8 of 9

JBerns
Advisor
Advisor

@norman.yuan,

 

Yes, I saw @_gile's reply right after I posted my reply to you. Looks like great resources and advice from you both.

Thank you both for your assistance.

 

Kind regards,

Jerry

-----------------------------------------------------------------------------------------
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
0 Likes
Message 9 of 9

techsoft3d
Contributor
Contributor

Jerry -

Looks like you have a solution to your problem already. I just thought I'd describe an alternative approach, maybe for the benefit of others. If you don't want to create a custom document tab, you can implement customized user interaction in zero document state using the Application menu.
The solution is based on setting the variables STARTMODE 0 and STARTUP 3.
AutoCAD will launch in zero document mode and the RIBBON component will be pre-initialized, which gives you access to the ComponentManager, which holds the ApplicationMenu.

Create a managed dll that gets loaded at AutoCAD startup, load control value 2.

 

//Global var for ZeroDocState
ApplicationMenuItem acApMenuItem = null;
public void Initialize()
{
if (ComponentManager.ApplicationMenu != null)
ComponentManager.ApplicationMenu.Opening += new EventHandler<EventArgs>(ApplicationMenu_Opening);

}
public void Terminate()
{
// Remove the application menu Opening event handler
if (acApMenuItem != null)
ComponentManager.ApplicationMenu.Opening -= new EventHandler<EventArgs>(ApplicationMenu_Opening);
}

 

void ApplicationMenu_Opening(object sender, EventArgs e)
{
// Check to see if the custom menu item was added previously
if (acApMenuItem == null)
{
// Get the application menu component
ApplicationMenu acApMenu = ComponentManager.ApplicationMenu;

// Create a new application menu item
acApMenuItem = new ApplicationMenuItem();
acApMenuItem.Text = "ZeroDoc Cmd";
acApMenuItem.LargeImage = getBitmap("CustomCmd.png");
acApMenuItem.ShowImage = true;
acApMenuItem.CommandHandler = new MyCommandHandler();

// Append the new menu item
acApMenu.MenuContent.Items.Add(acApMenuItem);

}
}

BitmapImage getBitmap(string fileName)
{
BitmapImage bmp = new BitmapImage();
//BitmapImage.UriSource must be in a BeginInit/ EndInit block.
bmp.BeginInit();
string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
bmp.UriSource = new Uri(path + @"\Support\" + fileName);
bmp.EndInit();
return bmp;
}

 

ZDAcad.JPG

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

ZDAcadCustom.JPG

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Image from an old OEM application, but same applies to vanilla AutoCAD.

 

 

 

 

 

In the command handler you'd call your custom code:

public class MyCommandHandler : System.Windows.Input.ICommand
{
public bool CanExecute(object parameter)
{
return true;
}

public event EventHandler CanExecuteChanged;

public void Execute(object parameter)
{
ModalForm modal = new ModalForm();
Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(modal);
}
}

With this approach you could even create a modeless form in the Initialize() function, a standard Palette using the AutoCAD.Windows.PaletteSet.

 

ModelessForm mf = new ModelessForm();
mf.testPalette();

 

public void testPalette()
{
try
{
if (ps == null)
{
ps = new Autodesk.AutoCAD.Windows.PaletteSet("Test Palette Set");
ps.Style = PaletteSetStyles.ShowAutoHideButton | PaletteSetStyles.ShowCloseButton;
ps.Opacity = 90;
ps.MinimumSize = new System.Drawing.Size(300, 300);
System.Windows.Forms.UserControl myCtrl = new ModelessForm();
ps.Add("test", myCtrl);
ps.Visible = false;
ps.Visible = true;
}
}
catch
{
}
}

Of course, the same limitations to custom,native command access as well as DWG related API functions, apply as in the provided custom document solution you already got from Norman Yuan.

 

Cheers,

Paavo Rantanen, Tech Soft 3D