.NET

Reply
New Member
tr100
Posts: 2
Registered: ‎06-22-2005
Message 1 of 2 (71 Views)

Why can't create object my code?

71 Views, 1 Replies
06-23-2005 07:22 PM
As a beginner,I met a problem,this is my code :
using System ;
using Autodesk.AutoCAD.Runtime ;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.EditorInput;

[assembly: CommandClass(typeof(ClassLibrary.Class))]

namespace ClassLibrary
{
///
/// Summary description for Class.
///

public class Class
{
public Class()
{
//
// TODO: Add constructor logic here
//
this.CreateEmployee();
}
//This function returns the ObjectId for the BlockTableRecord called "EmployeeBlock",
//creating it if necessary. The block contains three entities - circle, text
//and ellipse.
public ObjectId CreateEmployeeDefinition()
{
ObjectId newBtrId = new ObjectId(); //The return value for this function
Database db = HostApplicationServices.WorkingDatabase; //save some space
Transaction trans = db.TransactionManager.StartTransaction(); //begin the transaction
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;

try
{
//Now, drill into the database and obtain a reference to the BlockTable
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForWrite);
if ((bt.Has("EmployeeBlock")))
{
newBtrId =bt["EmployeeBlock"];
}
else
{
Point3d center = new Point3d(10, 10, 0); // convenient declaration...
// Declare and define the entities we want to add:
//Circle:
Circle circle = new Circle(center, Vector3d.ZAxis, 2);
//Text:
MText text = new MText();
text.Contents = "Earnest Shackleton";
text.Location = center;
//Ellipse:
Ellipse ellipse = new Ellipse(center, Vector3d.ZAxis, new Vector3d(10, 0, 0), 0.5, 0, 0);

//Next, create a layer with the helper function, and assign
//the layer to our entities.
ObjectId empId = CreateLayer();
text.LayerId = empId;
circle.LayerId = empId;
ellipse.LayerId = empId;
//Set the color for each entity irrespective of the layer's color.
text.ColorIndex = 2;
circle.ColorIndex = 1;
ellipse.ColorIndex = 3;

//Create a new block definition called EmployeeBlock
BlockTableRecord newBtr = new BlockTableRecord();
newBtr.Name = "EmployeeBlock";
newBtrId = bt.Add(newBtr); //Add the block, and set the id as the return value of our function
trans.AddNewlyCreatedDBObject(newBtr, true); //Let the transaction know about any object/entity you add to the database!

newBtr.AppendEntity(circle); //Append our entities...
newBtr.AppendEntity(text);
newBtr.AppendEntity(ellipse);
trans.AddNewlyCreatedDBObject(circle, true); //Again, let the transaction know about our newly added entities.
trans.AddNewlyCreatedDBObject(text, true);
trans.AddNewlyCreatedDBObject(ellipse, true);

}
trans.Commit(); //All done, no errors? Go ahead and commit!
}
catch
{
ed.WriteMessage("Error Creating Employee Block");
}
finally
{
trans.Dispose();
}
return newBtrId;

}


//This function creates a new BlockReference to the "EmployeeBlock" object,
//and adds it to ModelSpace.
[CommandMethod("x")]
public void CreateEmployee()
{
Database db = HostApplicationServices.WorkingDatabase;
Transaction trans = db.TransactionManager.StartTransaction();
try
{

BlockTable bt = (BlockTable)(trans.GetObject(db.BlockTableId, OpenMode.ForWrite));
BlockTableRecord btr =(BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
//Create the block reference...use the return from CreateEmployeeDefinition directly!
BlockReference br = new BlockReference(new Point3d(10, 10, 0), CreateEmployeeDefinition());
btr.AppendEntity(br); //Add the reference to ModelSpace
trans.AddNewlyCreatedDBObject(br, true); //Let the transaction know about it
trans.Commit();
}
finally
{
trans.Dispose();
}
}

//This function returns the objectId for the "EmployeeLayer", creating it if necessary.
public ObjectId CreateLayer()
{
ObjectId layerId;
Database db = HostApplicationServices.WorkingDatabase;
Transaction trans = db.TransactionManager.StartTransaction();
//Get the layer table first...
LayerTable lt = (LayerTable)trans.GetObject(db.LayerTableId, OpenMode.ForWrite);
//Check if EmployeeLayer exists...
if (lt.Has("EmployeeLayer"))
{
layerId = lt["EmployeeLayer"];
}
else
{
//If not, create the layer here.
LayerTableRecord ltr = new LayerTableRecord();
ltr.Name = "EmployeeLayer"; // Set the layer name
ltr.Color = Color.FromColorIndex(ColorMethod.ByAci, 2);
layerId = lt.Add(ltr);
trans.AddNewlyCreatedDBObject(ltr, true);
}
trans.Commit();
trans.Dispose();
return layerId;
}

}
}

//can you find any error? why circle and ntext can't create?
New Member
tr100
Posts: 2
Registered: ‎06-22-2005
Message 2 of 2 (71 Views)

Re: Why can't create object my code?

06-23-2005 07:27 PM in reply to: tr100
sorry,this is my code, :smileyhappy: copy error
using System ;
using Autodesk.AutoCAD.Runtime ;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.EditorInput;

[assembly: CommandClass(typeof(ClassLibrary.okClass))]

namespace ClassLibrary {
///
/// Summary description for okClass.
///

public class okClass {
public okClass() {
//
// TODO: Add constructor logic here
//
this.CreateObject();
}

// Define Command "create"
[CommandMethod("2")]
public void test() { // This method can have any name
// Put your command code here

}

public ObjectId CreateLayer(){
ObjectId layerid;
Database db=HostApplicationServices.WorkingDatabase;
Transaction trans=db.TransactionManager.StartTransaction();

LayerTable lt=(LayerTable)trans.GetObject(db.LayerTableId,OpenMode.ForWrite);
if(lt.Has("EmployeeLayer")){
layerid=lt["EmployeeLayer"];
}else{
LayerTableRecord ltr=new LayerTableRecord();
ltr.Name="ne layer!";
layerid=lt.Add(ltr);
trans.AddNewlyCreatedDBObject(ltr,true);
}
trans.Commit();
trans.Dispose();
return layerid;
}

public void CreateObject(){
ObjectId newBtrId=new ObjectId();
Database db=HostApplicationServices.WorkingDatabase;
Transaction trans=db.TransactionManager.StartTransaction();
Editor ed=Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;

try{
BlockTable bt=(BlockTable)trans.GetObject(db.BlockTableId,OpenMode.ForWrite);
if(bt.Has("EmployeeBlock")){
newBtrId=bt["my block"];
}else{
ObjectId tempId=this.CreateLayer();

Circle circle=new Circle(new Point3d(10,10,0),Vector3d.ZAxis,50);
circle.LayerId=tempId;
circle.ColorIndex=1;

MText tname=new MText();
tname.Contents="hello!!" ;
tname.LayerId=tempId;
tname.ColorIndex=2;
tname.Location=new Point3d(10,10,0);

BlockTableRecord btr=new BlockTableRecord();
btr.Name="我的block";
newBtrId=bt.Add(btr);
trans.AddNewlyCreatedDBObject(btr,true);

btr.AppendEntity(circle);
btr.AppendEntity(tname);
trans.AddNewlyCreatedDBObject(circle,true);
trans.AddNewlyCreatedDBObject(tname,true);

trans.Commit();
ed.WriteMessage("end!!!");
}
}catch{
ed.WriteMessage("error!!");
}finally{
trans.Dispose();
}
}
}
}
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are You Going To Be @ AU 2014? Feel free to drop by our AU topic post and share your plans, plug a class that you're teaching, or simply check out who else from the community might be in attendance. Ohh and don't forgot to stop by the Autodesk Help | Learn | Collaborate booths in the Exhibit Hall and meet our community team if you get a chance!