.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Reply
Message 1 of 4
Anonymous
2236 Views, 3 Replies

C# example

what extension does a c# program need to be in order to be used in AutCad.
her is what i have

using System;

using System.Reflection;

using System.Runtime.InteropServices;

using Autodesk.AutoCAD.Runtime;

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.EditorInput;

using acadApp = Autodesk.AutoCAD.ApplicationServices.Application;

public class HelloWorld

{

[Autodesk.AutoCAD.Runtime.CommandMethod("HELLO")]

public void HelloCommand()

{

acadApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage("Hello World
from C#!");

acadApp.UpdateScreen();

}


[Autodesk.AutoCAD.Runtime.CommandMethod("HELLOTEXT")]

public void HelloTextCommand()

{

Database acadDB = HostApplicationServices.WorkingDatabase;

Autodesk.AutoCAD.DatabaseServices.TransactionManager acadTransMgr =
acadDB.TransactionManager;

Transaction acadTrans = acadTransMgr.StartTransaction();

MText acadMText = new MText();

try

{

BlockTableRecord acadBTR =
(BlockTableRecord)acadTrans.GetObject(acadDB.CurrentSpaceId,
OpenMode.ForWrite);

acadMText.Contents = "Hello World from C#!!!";

acadBTR.AppendEntity(acadMText);

acadTrans.AddNewlyCreatedDBObject(acadMText, true);

acadTrans.Commit();

}

catch

{

acadTrans.Abort();

}

finally

{

if (acadMText != null)

acadMText.Dispose();

}

}

}





sorry i'm just learning.
3 REPLIES 3
Message 2 of 4
pavlos.katsonis
in reply to: Anonymous

Compile it to a DLL (class library) and load it in acad with netload, but, read the help files before asking a question.
Message 3 of 4
Anonymous
in reply to: Anonymous

Richard, its easier to work with -- and read -- pasted code when using
PureText [1] which is a nice tool that sits in the TaskTray.

<%= Clinton Gallagher
NET csgallagher AT metromilwaukee.com
URL http://clintongallagher.metromilwaukee.com/
MAP http://wikimapia.org/#y=43038073&x=-88043838&z=17&l=0&m=h

[1] http://www.stevemiller.net




"richard" wrote in message
news:5384404@discussion.autodesk.com...
what extension does a c# program need to be in order to be used in AutCad.
her is what i have

using System;

using System.Reflection;

using System.Runtime.InteropServices;

using Autodesk.AutoCAD.Runtime;

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.EditorInput;

using acadApp = Autodesk.AutoCAD.ApplicationServices.Application;

public class HelloWorld

{

[Autodesk.AutoCAD.Runtime.CommandMethod("HELLO")]

public void HelloCommand()

{

acadApp.DocumentManager.MdiActiveDocument.Editor.WriteMessage("Hello World
from C#!");

acadApp.UpdateScreen();

}


[Autodesk.AutoCAD.Runtime.CommandMethod("HELLOTEXT")]

public void HelloTextCommand()

{

Database acadDB = HostApplicationServices.WorkingDatabase;

Autodesk.AutoCAD.DatabaseServices.TransactionManager acadTransMgr =
acadDB.TransactionManager;

Transaction acadTrans = acadTransMgr.StartTransaction();

MText acadMText = new MText();

try

{

BlockTableRecord acadBTR =
(BlockTableRecord)acadTrans.GetObject(acadDB.CurrentSpaceId,
OpenMode.ForWrite);

acadMText.Contents = "Hello World from C#!!!";

acadBTR.AppendEntity(acadMText);

acadTrans.AddNewlyCreatedDBObject(acadMText, true);

acadTrans.Commit();

}

catch

{

acadTrans.Abort();

}

finally

{

if (acadMText != null)

acadMText.Dispose();

}

}

}





sorry i'm just learning.
Message 4 of 4
asmaa14
in reply to: Anonymous

tu peux utilisé les cette commande

AcadApplication app = new AcadApplication();

AcadDocument doc1 = app.ActiveDocument;

for exemple: to create a line

using System;
using Autodesk.AutoCAD.Runtime;
using System.Collections.Generic;
using System.Windows.Forms;
using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Interop.Common;
using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;

[CommandMethod("line")]
public void line() // This method can have any name
{

AcadApplication app = new AcadApplication();

AcadDocument doc1 = app.ActiveDocument;


//This example adds a line in model space


AcadLine lineObj;

double[] startPoint = new double[3];

double[] endPoint = new double[3];


//Define the start and end points for the line

startPoint[0] = 1; startPoint[1] = 1; startPoint[2] = 0;

endPoint[0] = 50; endPoint[1] = 200; endPoint[2] = 0;

//Create the line in model space

lineObj = doc1.ModelSpace.AddLine(startPoint, endPoint);


app.Application.Visible = true;

doc1.Regen(AcRegenType.acAllViewports);

app.ZoomAll();

}

ou bien vous pouvez appeler la DLL dans autocad en utilisant la commande "netload"

voici un autre exemple:

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using System;
using Autodesk.AutoCAD.Interop.Common;

namespace Linetype
{

public class Commands
{

[CommandMethod("line")]

public void CreateComplexLinetype()
{

Document doc = Application.DocumentManager.MdiActiveDocument;

Database db = doc.Database;

Editor ed = doc.Editor;

Transaction tr2 = db.TransactionManager.StartTransaction();


using (tr2)
{

BlockTable bt = (BlockTable)tr2.GetObject(db.BlockTableId, OpenMode.ForRead);

BlockTableRecord btr = (BlockTableRecord)tr2.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);

PromptPointOptions opts = new PromptPointOptions("\nEnter start Point:");

PromptPointResult res = ed.GetPoint(opts);

PromptPointOptions opts2 = new PromptPointOptions("\nEnter end Point:");

PromptPointResult res2 = ed.GetPoint(opts2);

Line ln = new Line(res.Value, res2.Value);


ln.SetDatabaseDefaults(db);

Color currentcolor = new Color();

ln.Color = Color.FromRgb(0, 255, 0);


btr.AppendEntity(ln);

tr2.AddNewlyCreatedDBObject(ln, true);

tr2.Commit();
}

}



}

}

suivez les étapes suivantes:

1-creer un nouveau projet en visuel studio-->new-->project-->visuel c#-->AutoCAD Managed C# Project,
2-copier le code source,
3-compiler l'application (build or F7)
4-lancer autocad
5-exécuter la commande "netload"
6-charger votre dl dans autocad
7-tester votre commande "line" (créer par la dll )dans autocad tj.

j'espère que ça va marcher ,
bon courage.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost