MultiCAD.NET API:.dwg図面に非グラフィック情報を保存する

画像



すべてのCADアプリケーション開発者は、遅かれ早かれ、図面に補助的な非グラフィック情報を保存する問題に直面します。 これは、個々のグラフィック要素の属性、個々のシートの属性、または図面全体の設定です。 ブロック属性とは異なり、この情報はユーザーには表示されず、図面のソフトウェア処理に使用されます。



今日、問題を解決するための伝統的な方法がいくつかあります。XDataを描画要素に追加し、XRecordを使用し、独自の非グラフィックオブジェクトを作成します。



従来のものと比較して、MultiCAD.NET APIで非グラフィック情報を作成および保存するメカニズムは、はるかにコンパクトで使いやすいです。 さらに、これは普遍的であり、グラフィック要素、シート、図面自体など、図面内のさまざまなタイプのデータに等しく適用できます。 さまざまなタイプのデータを追加情報として使用できます。



それがどのように機能し、実際にどのように適用されるかは、カットの下を参照してください。





グラフィックオブジェクトへのカスタムプロパティの追加



オブジェクトのデータを操作するには、 CustomProperties



プロパティをCustomProperties



。これは、 McPropertySource



クラスのすべての子孫で使用できます。特に、データベース内のすべてのグラフィックプリミティブ( McDbEntity



クラスのインスタンス)です。 このプロパティを使用すると、キーと値のペアの形式でデータを追加および読み取ることができます。



 entity.DbEntity.CustomProperties["Property"] = Value;
      
      





単純型は、単純型の配列と同様にプロパティ値として使用できます。 実際には、オブジェクトのシリアル化を使用して任意のタイプのプロパティを追加し、データをバイト配列に書き込むことができます。 次の例は、データ辞書をMyCustomProperty



ネイティブプロパティの値として保存する例を示しています。



 MemoryStream ms = new MemoryStream(); BinaryFormatter formatter = new BinaryFormatter(); Dictionary<String, Object> MyOptions = new Dictionary<String, Object>(); MyOptions.Add("param1", 10); MyOptions.Add("param2", "Value"); try { formatter.Serialize(ms, MyOptions); } catch (SerializationException e) { Console.WriteLine("Failed to serialize. Reason: " + e.Message); } Byte[] binary = ms.GetBuffer(); entity.DbEntity.CustomProperties["MyCustomProperty"] = binary;
      
      





「複雑な」データの読み取りは、2つの段階で行われます。バイト配列の取得と、逆シリアル化です。 CustomProperties



常にリストの形式で配列の内容を返します:



 List<Byte> loadedBinary = entity.DbEntity.CustomProperties["MyCustomProperty "] as List<Byte>; if (loadedBinary != null) { ms = new MemoryStream(loadedBinary.ToArray()); formatter = new BinaryFormatter(); MyOptions = formatter.Deserialize(ms) as Dictionary<String, Object>; int val = (int)MyOptions["param1"]; String str = MyOptions["param2"] as String; }
      
      





特定の例でこのメソッドを適用することを検討してください。 .dwgファイルに、給湯と冷水のパイプラインがポリラインで表される給水スキームが含まれているとします。 パイプラインの目的とパイプの直径に関する情報を含む説明を個々のポリラインに追加します。



[パイプタイプ] =冷水

[パイプ直径] = 20



ポリラインオブジェクトのカスタム選択を行うコマンドを登録し、それにキー/値のペアを追加します。



 [CommandMethod("WriteCustomProperties", CommandFlags.NoCheck | CommandFlags.NoPrefix)] static public void writeCold50() { McObjectId objId = McObjectManager.SelectObject("Select a polyline entity to write additional data to: "); if (objId.GetObject().IsKindOf(DbPolyline.TypeID)) { McEntity ent = objId.GetObject(); ent.DbEntity.CustomProperties["Pipe type"] = "Cold water"; ent.DbEntity.CustomProperties["Pipe diameter"] = 50.0; } else { MessageBox.Show("No polyline entity selected"); } }
      
      





また、 McPropertySource



クラスのすべての子孫について、 GetProperties()



メソッドを使用して、特定のタイプ(カスタム、オブジェクト、ユーザーなど)の使用可能なすべてのプロパティを取得できます。 次のコマンドは、図面で選択されたプリミティブのすべてのカスタムプロパティのリストを受け取り、それらの名前を画面に表示します。



 [CommandMethod("GetCustomProperties", CommandFlags.NoCheck | CommandFlags.NoPrefix)] static public void getCustomProperties() { McObjectId objId = McObjectManager.SelectObject("Select an entity to get its custom property list: "); if (objId.IsNull) { MessageBox.Show("No entity selected"); return; } String propertyString = null; List<McProperty> customPropertyNames = new List<McProperty>(); McEntity ent = objId.GetObject(); customPropertyNames = ent.DbEntity.GetProperties(McProperties.PropertyType.Custom).GetProps(); foreach (McProperty property in customPropertyNames) { propertyString = propertyString + property.Name + ";\n"; } MessageBox.Show(propertyString, "Custom property list"); }
      
      





ドキュメントの非グラフィック情報を保存する



MultiCAD.NETでは、プリミティブだけでなく、ドキュメント全体および個々のシートとブロック(サブドキュメント)の非グラフィック情報も保存できます。 McDocument



クラスはMcPropertySource



の機能も継承するため、同じCustomProperties



プロパティを使用して、異なるレベルのドキュメントに独自のデータを設定できます。



 [CommandMethod("WriteCustomPropertiesToDoc", CommandFlags.NoCheck | CommandFlags.NoPrefix)] static public void writeCustomPropertiesToDoc() { McDocument currentLayout = McDocumentsManager.GetActiveSheet(); currentLayout.CustomProperties["Layout Property 1"] = "Value"; McDocument currentDocument = McDocumentsManager.GetActiveDoc(); currentDocument.CustomProperties["Document Property 1"] = "Value"; }
      
      





具体的な例でこれがどのように機能するかを見てみましょう。 前回の記事で、 MultiCAD.NETを使用してカスタムプリミティブを作成し、長方形フレーム内のテキストであるTextInBoxプリミティブを処理することについて説明しました。



画像



独自のドキュメントプロパティを使用して、このドキュメントのパラメータと設定を設定できます。 たとえば、この場合、現在のドキュメントに存在するすべてのTextInBoxプリミティブのフレームとテキスト文字列の色設定:



 McDocument currentDocument = McDocumentsManager.GetActiveDoc(); currentDocument.CustomProperties["BoxColor"] = "Blue"; currentDocument.CustomProperties["TextColor"] = "Green";
      
      





OnDraw()



関数を書き換えて、カスタムプリミティブの描画を行い、要素の色がドキュメントの設定プロパティから読み取られるようにします。



 public override void OnDraw(GeometryBuilder dc) { dc.Clear(); dc.Color = Color.FromName(currentDocument.CustomProperties["BoxColor"] as String); dc.DrawPolyline(new Point3d[] { _pnt1, new Point3d(_pnt.X, _pnt2.Y, 0), _pnt2, new Point3d(_pnt2.X, _pnt.Y, 0), _pnt1}); dc.TextHeight = 2.5 * DbEntity.Scale; dc.Color = Color.FromName(currentDocument.CustomProperties["TextColor"] as String); dc.DrawMText(new Point3d((_pnt2.X + _pnt.X) / 2.0, (_pnt2.Y + _pnt.Y) / 2.0, 0), Vector3d.XAxis, Text, HorizTextAlign.Center, VertTextAlign.Center); }
      
      





したがって、追加されたすべてのTextInBoxプリミティブは、現在のドキュメントの指定されたプロパティに従って表示されます。



記事に関する議論は、フォーラム( forum.nanocad.ru/index.php?showtopic=6517)でも利用できます。

記事の英語への翻訳: MultiCAD.NET API:非グラフィックデータを.dwg図面に保存します



All Articles