• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    .NET

    Reply
    New Member
    Posts: 2
    Registered: ‎06-22-2005

    Why can't create object my code?

    59 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?
    Please use plain text.
    New Member
    Posts: 2
    Registered: ‎06-22-2005

    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();
    }
    }
    }
    }
    Please use plain text.