Monday, 15 April 2013

How to call Lisp Program from VB.NET ? || Calling AutoCAD commands from .NET

                                    Hi Friends, in this article I will explain about how to open AutoLISP file through windows forms.
Suppose we are working on VB.NET windows forms ,suddenly we call the LISP function from VB.NET windows form then it use because we already the code in AutoLISP and rewritten the LISP file in VB.NET its time waste so simply call the AutoLISP command from VB.NET is very easy compare to rewrite.
I already explained in the article How to open new AutoCAD drawing using VB.NET or C#? Or How to Connect with AutoCAD and Open A Drawing in VB.NET /C#
How to create a project in VB.NET?
I explain the create the project in screenshots, follow below screenshots.
1.Create new project ,File--> New Project

2.Select windows form application template and give project name and click ok.
3.The project will look like below figure.
4.Add references ,right click on the project and select Add reference.
5.Add AutoCAD 2007/2010 Type Library and AutoCAD/ObjectDBX Common 17.0/18.0 type Library from COM tab.
6.Add Reference --> acdbmgd.dll 
7.Add reference --> acmgd.dll
write the below code to call the lisp file from VB.NET or C#.

VB.NET Code:
Imports Autodesk.AutoCAD.Interop
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.Interop.Common
Public Class Form1
    Private Sub LoadLISPFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadLISPFile.Click
    End Sub
    Public Sub LoadLisp()
        Dim appver As Object = TryCast(My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\Autodesk\AutoCAD", "CurVer", Nothing), Object)
        If appver Is Nothing Then
            MessageBox.Show("Its not working ,have a some error ")
        End If
        Dim AcadApp As AcadApplication = New AcadApplication
            If AcadApp Is Nothing Then
                AcadApp = GetObject(, "AutoCAD.Application" + appver.ToString())
            End If
        Catch ex As System.Exception
            MessageBox.Show("Working in opened document only!")
        End Try
        AcadApp.Visible = True
        AcadApp.WindowState = AcWindowState.acMax
        Dim AcDoc = AcadApp.ActiveDocument
        Dim command As String = "(load ""D:/alert.lsp"")"
        AcDoc.SendCommand(command & "(C:alert) ")
    End Sub
End Class

C# Code:
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Interop.Common;
Public Class Form1
      private void LoadLISPFile_Click(System.Object sender, System.EventArgs e)
      public void LoadLisp()
            object appver = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\\Software\\Autodesk\\AutoCAD", "CurVer", null) as object;
            if (appver == null) {
                  MessageBox.Show("Its not working ,have a some error ");
            AcadApplication AcadApp = new AcadApplication();
            try {
                  if (AcadApp == null) {
                        AcadApp = Interaction.GetObject(, "AutoCAD.Application" + appver.ToString());
            } catch (System.Exception ex) {
                  MessageBox.Show("Working in opened document only!");
            AcadApp.Visible = true;
            AcadApp.WindowState = AcWindowState.acMax;
            dynamic AcDoc = AcadApp.ActiveDocument;
            string command = "(load \"D:/alert.lsp\")";
            AcDoc.SendCommand(command + "(C:alert) ");

When we run the above application or just click on play button or press  F5 then automatically AutoCAD opens and execute the code in alert.LSP file.

Thanks for reading my Aspdotnet-Roja BLOG..


  1. Hi Kishore,

    Nice Article ,very detail and unique,thanks for sharing .

    Dotnetnuke cms

    1. Thanks for your complement Dotbetnuke cms,your complements give extra energy to write more articles.


© 2012-2018 All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.