ドキュメント オブジェクトには、AutoCAD Civil 3D 図面要素(ポイントや線形など)のコレクションだけでなく、これらの要素を変更するオブジェクト(スタイルやラベル スタイルなど)が含まれています。CivilDocument のコレクションは、ほとんどのオブジェクトの場合、ObjectID コレクション(Autodesk.AutoCAD.DatabaseServices.ObjectIdCollection)です。これらのコレクションのオブジェクトは、Transaction.GetObject() を使用して取得し、使用する前にそのタイプにキャストする必要があります。
COM API では、ドキュメント オブジェクトは、キャストする必要のないオブジェクトのコレクションに含まれています。
ObjectIdCollection オブジェクトは IList インタフェースを実装します。このオブジェクトはインデックスによる列挙およびアクセスが可能です。foreach を使用してコリドー コレクションを反復し、結果の ObjectId を取得して Corridor にキャストすることで、そのメソッドおよびプロパティにアクセスする例を次に示します。
public static void iterateCorridors () { CivilDocument doc = CivilApplication.ActiveDocument; using ( Transaction ts = Application.DocumentManager.MdiActiveDocument. Database.TransactionManager.StartTransaction() ) { foreach ( ObjectId objId in doc.CorridorCollection ) { Corridor oCorridor = ts.GetObject(objId, OpenMode.ForRead) as Corridor; Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("Corridor: {0}\nLargest possible triangle side: {1}\n", oCorridor.Name, oCorridor.MaximumTriangleSideLength); } } }
ObjectIdCollection の詳細は、 『Civil 3D .NET API リファレンス』を参照してください。
次の例では、新しいポイント スタイルを作成します。
ObjectId pointStyleID = doc.Styles.PointStyles.Add("Name"); // Now a new point style is added to the collection of styles, // and we can modify it by setting the properties // of the oPointStyle object, which we get from the Transaction ts: PointStyle oPointStyle = ts.GetObject(pointStyleID, OpenMode.ForWrite) as PointStyle; oPointStyle.Elevation = 114.6; // You must commit the transaction for the add / modify operation // to take effect ts.Commit();
既存の要素とプロパティが一致する要素を追加しようとした場合、存在しない項目にアクセスしようとした場合、または存在しないか使用されていない項目を削除しようとした場合は、エラーが発生します。エラーをトラップし、適切に応答する必要があります。
次の例は、こうしたエラーを処理する方法の 1 つを示しています。
// Try to access the style named "Name" try { // This raises an ArgumentException if the item doesn't // exist: ObjectId pointStyleId = doc.Styles.PointStyles["Name"]; // do something with the point style... } catch ( ArgumentException e ) { ed.WriteMessage(e.Message); }