18 June 2013

How to send a command from a VB.NET windows forms to AutoCAD? || How to LISP command from VB.NET windows forms?


Hi Friends, in this article I will explain about how to open LISP file through vb.net 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.

How to create a project in VB.NET ?
I explain the create the project in screenshots, follow below screenshots.
1.File -->click on  New Project


2.Select Your language in left hand side and select windows application and give project name ,i gave as OpenLispCommand

3.Form will like below figure.

4.Add Autocad references like below figure.

5.Go to COM and add AutoCAD 2007 type library and AutoCAD/ObjectDBX Common 17.0 Type...


6.Add another 2 libraries browse --> c:\\program files add acdbmgd.dll and acmgd.dll 





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
        Me.Hide()
        LoadLisp()
        Me.Show()
    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 ")
            Return
        End If
        Dim AcadApp As AcadApplication = New AcadApplication
        Try
            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!")
            Return
        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)
      {
            this.Hide();
            LoadLisp();
            this.Show();
      }
      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 ");
                  return;
            }
            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!");
                  return;
            }
            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 F5 then automatically autocad opens and execute the code in alert.LSP file.

2 comments:

  1. Would have been nice if we could copy and paste this code!

    ReplyDelete