How to create a Shape object in code C# (shp in shx file) in autocad

How to create a Shape object in code C# (shp in shx file) in autocad

Anonymous
Not applicable
1,373 Views
1 Reply
Message 1 of 2

How to create a Shape object in code C# (shp in shx file) in autocad

Anonymous
Not applicable

How to insert a shp object from the shx file with the c # code, just like the SHAPE command:

 

Command: SHAPE
Enter shape name or [?]: ?

 

Enter shape name(s) to list <*>:

 

Available shapes:

 

File: ltypeshp.shx
TRACK1

ZIG
BOX

CIRC1
BAT

AMZIGZAG

 

 

After inserting such an object and reading it, we have to deal with the object Autodesk.AutoCAD.DatabaseServices.Shape. 

 

Nowhere did I find information on how to create such objects using the c # code.

 

Please help!

 

0 Likes
Accepted solutions (1)
1,374 Views
1 Reply
Reply (1)
Message 2 of 2

Alexander.Rivilis
Mentor
Mentor
Accepted solution

This code can help you:

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;

// This line is not mandatory, but improves loading performances
[assembly: CommandClass(typeof(InsertShape.MyCommands))]

namespace InsertShape
{
  public class MyCommands
  {
    [CommandMethod("InsertShape")]
    public void MyCommand() // This method can have any name
    {
      // Put your command code here
      Document doc = Application.DocumentManager.MdiActiveDocument;
      if (doc == null)
        return;
      Editor ed = doc.Editor;
      Database db = doc.Database;
      PromptStringOptions prStr =
        new PromptStringOptions("\nshx-file name: ");
      prStr.AllowSpaces = true;
      prStr.DefaultValue = "ltypeshp.shx";
      PromptResult rsStr = ed.GetString(prStr);
      if (rsStr.Status != PromptStatus.OK)
        return;
      string shxFilename = rsStr.StringResult;

      try
      {
        shxFilename = HostApplicationServices.Current
          .FindFile(shxFilename, db, FindFileHint.FontFile);
      } catch
      {
        ed.WriteMessage("\nShx-file not found!");
        return;
      }

      prStr.DefaultValue = "BOX";
      prStr.Message = "\nShape name: ";
      rsStr = ed.GetString(prStr);
      if (rsStr.Status != PromptStatus.OK)
        return;
      string shapeName = rsStr.StringResult;

      PromptPointResult rsPnt =
        ed.GetPoint("\nInsert point: ");
      if (rsPnt.Status != PromptStatus.OK)
        return;

      using (Transaction tr = doc.TransactionManager.StartTransaction())
      {
        TextStyleTable tst = tr.GetObject(db.TextStyleTableId, OpenMode.ForWrite) as TextStyleTable;
        ObjectId styleId = GetObjectIdOfTextStyle(tst, shxFilename, tr);
        if (styleId.IsNull)
        {
          TextStyleTableRecord tstr = new TextStyleTableRecord();
          tstr.IsShapeFile = true;
          tstr.FileName = shxFilename;
          styleId = tst.Add(tstr);
          tr.AddNewlyCreatedDBObject(tstr, true);
        }
        Shape shape = new Shape(rsPnt.Value, 1.0, 0.0, 1.0);
        shape.SetDatabaseDefaults(db);
        shape.StyleId = styleId;
        BlockTableRecord btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
        btr.AppendEntity(shape);
        shape.Name = shapeName;
        tr.AddNewlyCreatedDBObject(shape, true);
        tr.Commit();
      }
    }
    public static ObjectId GetObjectIdOfTextStyle(TextStyleTable tst, string shxFileName, Transaction tr)
    {
      foreach (ObjectId idtsr in tst)
      {
        TextStyleTableRecord tstr =
          tr.GetObject(idtsr, OpenMode.ForRead) as TextStyleTableRecord;
        if (shxFileName.ToLower() == tstr.FileName.ToLower())
          return tstr.ObjectId;
      }
      return ObjectId.Null;
    }
  }
}

 

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member