I will try to explain.
I create a new code (windows form or class) in visual studio c#.
Solution Explorer/ right mouse on namespace / properties
Debug / start external program / here we choose file to autocad.
So when I press F5 new autocad file opens, it takes about 30seconds.
I want to write a program withou setting external program in a debug – is it possible.
Instead of starting external program I would like myself open
autocad file e:\strop.dwg
When I press F5 C# code catches this active cad file (strop.dwg) ,
I wrote myself netload or put command in c#
SendCommand("_Netload");
I choose my dll file, I write name of command method.
When the c# finishes, I Stop debugging and the house.dwg is still
open.
I read about it on this forum , I wrote this program, please correct this
I use visual studio 2010, autocad 2009.
I read this article, it doesn't work. I missed something
http://forums.autodesk.com/t5/NET/using-COM-API-in-net-dll/m-p/1334401/highlight/true#M153
This is my code visual 2010 c#
Please correct and enclose visual studio files as a solution.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.ComponentModel;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.GraphicsInterface;
using Autodesk.AutoCAD.GraphicsSystem;
using Autodesk.AutoCAD.LayerManager;
using Autodesk.AutoCAD.PlottingServices;
using Autodesk.AutoCAD.Publishing;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Windows;
using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Interop.Common;
using Aplikacja = Autodesk.AutoCAD.ApplicationServices.Application;
namespace CadConnectwf
{
public partial class HitAcad : Form
{
public AcadApplication cadApp;
public AcadDocument cadDoc;
public HitAcad()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Connect2Acad();
}
public bool Connect2Acad()
{
// Autodesk.AutoCAD.Interop.AcadApplication _AcadApp = (AcadApplication)Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication;
try
{
cadApp =(Autodesk.AutoCAD.Interop.AcadApplication)
Marshal.GetActiveObject("AutoCAD.Application.17");
}
catch
{
try
{
cadApp =new Autodesk.AutoCAD.Interop.AcadApplication();
cadApp.Visible = true;
}
catch (System.Exception AcadEx)
{
string sMsg = AcadEx.Message + " from: " + AcadEx.Source;
MessageBox.Show(sMsg, "AutoCAD_BasicCalls|Connect2Acad",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
return true;
}
}
}
In help from autocad I found this but I can’t run it I suppose it is windows form.
Please enclose a vs studio file as a solution.
[CommandMethod("ConnectToAcad")]
public static void ConnectToAcad()
{
AcadApplication acAppComObj = null;
// "AutoCAD.Application.17" uses 2007 or 2008,
const string strProgId = "AutoCAD.Application.17";
// Get a running instance of AutoCAD
try
{
acAppComObj = (AcadApplication)Marshal.GetActiveObject(strProgId);
}
catch // An error occurs if no instance is running
{
try
{
// Create a new instance of AutoCAD
acAppComObj = (AcadApplication)Activator.CreateInstance(Type.GetTypeFromProgID(strProgId), true);
}
catch
{
// If an instance of AutoCAD is not created then message and exit
// System.Windows.Forms.MessageBox.Show("Instance of 'AutoCAD.Application'" + " could not be created.");
return;
}
}
// Display the application and return the name and version
acAppComObj.Visible = true;
// System.Windows.Forms.MessageBox.Show("Now running " + acAppComObj.Name + " version " + acAppComObj.Version);
// Get the active document
AcadDocument acDocComObj;
acDocComObj = acAppComObj.ActiveDocument;
// Optionally, load your assembly and start your command or if your assembly
// is demandloaded, simply start the command of your in-process assembly.
acDocComObj.SendCommand("(command " + (char)34 + "NETLOAD" + (char)34 + " " + (char)34 + "g:/nauka c# cad/CsMgd1/bin/debug/CsMgd1.dll" + (char)34 + ") ");
acDocComObj.SendCommand("MyCommand ");
}