Autodesk Community Tips- ADNオープン
Autodesk Community Tipsではちょっとしたコツ、やり方、ショートカット、アドバイスやヒントを共有しています。

AutoCAD .NET API :オブジェクトを参照するフィールドの作成

Issue

FIELD コマンドで選択したオブジェクトの面積をフィールド文字として作成することは出来ますか?

 

field.jpg

field_format.jpg 

  

Solution

AutoCAD .NET API では、文字(DBText)または、マルチテキスト(MText)ブロックを作成後、[フィールド] ダイアログ下部に表示される「フィールド式」を SetField メソッドで設定することで、特定のオブジェクトを参照するフィールド文字を作成することが出来ます。

 

field_expression.jpg

 

フィールド式中の _ObjId の値は、選択したオブジェクトの ObjectId 値になります。

次の C# コードは、ポリラインの面積をフィールド文字として作図する例です。

 

  [CommandMethod("MyCommand")]
  public void MyCommand() // This method can have any name
  {
      Document doc = Application.DocumentManager.MdiActiveDocument;
      Database db = doc.Database;
      Editor ed = doc.Editor;

      PromptEntityOptions options = new PromptEntityOptions("\nポリラインを選択:");
      options.SetRejectMessage("\nポリラインを選択");
      options.AddAllowedClass(typeof(Autodesk.AutoCAD.DatabaseServices.Polyline), false);
      PromptEntityResult acSSPrompt = ed.GetEntity(options);
      if (acSSPrompt.Status != PromptStatus.OK)
        return;

      PromptPointOptions ppo = new PromptPointOptions("\n挿入点を指示:");
      PromptPointResult ppr = ed.GetPoint(ppo);
      if (ppr.Status != PromptStatus.OK)
        return;

      using (Transaction tr = db.TransactionManager.StartTransaction())
      {
        // %<\AcObjProp.16.2 Object(%<\_ObjId 3132455955584>%).Area \f "%lu2%pr1%ps[面積:, ㎡]%ct8[0.001]">%
        string strId = acSSPrompt.ObjectId.OldIdPtr.ToString();
        string str1 = "%<\\AcObjProp.16.2 Object(%<\\_ObjId ";
        string str2 = ">%).Area \\f \"%lu2%pr1%ps[面積:, ㎡]%ct8[0.001]\">%";
        string format = str1 + strId + str2;

        DBText text = new DBText();
        text.Height = 10.0;
        text.Position = ppr.Value;
        ObjectId objId = SymbolUtilityServices.GetBlockModelSpaceId(db);

        BlockTableRecord btr = tr.GetObject(objId, OpenMode.ForWrite) as BlockTableRecord;
        btr.AppendEntity(text);
        tr.AddNewlyCreatedDBObject(text, true);
        Field entField = new Field(format);
        entField.Evaluate();
        text.SetField(entField);
        tr.AddNewlyCreatedDBObject(entField, true);
        tr.Commit();
      }
  }

 

作図したフィールド文字は、参照したポリラインの変化に追従するようになります。

 

field.gif