.NET

Reply
*richard
Message 1 of 4 (622 Views)

C# example

622 Views, 3 Replies
11-03-2006 08:13 AM
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.
Valued Contributor
pavlos.katsonis
Posts: 67
Registered: ‎09-12-2006
Message 2 of 4 (622 Views)

Re: C# example

11-03-2006 08:56 AM in reply to: *richard
Compile it to a DLL (class library) and load it in acad with netload, but, read the help files before asking a question.
*clintonG
Message 3 of 4 (622 Views)

Re: C# example

11-03-2006 06:41 PM in reply to: *richard
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.
Active Member
asmaa14
Posts: 6
Registered: ‎05-20-2008
Message 4 of 4 (622 Views)

Re: C# example

09-21-2008 06:57 AM in reply to: *richard
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.

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community