CustomProperties
property is CustomProperties
, which is available for all descendants of the McPropertySource
class, which, in particular, are all graphic primitives in the database (instances of the McDbEntity
class). The property allows you to add and read data in the form of key-value pairs: entity.DbEntity.CustomProperties["Property"] = Value;
MyCustomProperty
property: 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;
CustomProperties
always returns the contents of the arrays in the List view: 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; }
[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
class, McPropertySource
can get all the available properties of a particular type (Custom, Object, User, etc.) using the GetProperties()
method. The following command gets a list of all custom-properties for the selected primitive in the drawing and displays their names on the screen. [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"); }
McDocument
class also inherits the functionality of McPropertySource
, and therefore, you can use the same CustomProperties
property to set your own data for documents at various levels: [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"; }
McDocument currentDocument = McDocumentsManager.GetActiveDoc(); currentDocument.CustomProperties["BoxColor"] = "Blue"; currentDocument.CustomProperties["TextColor"] = "Green";
OnDraw()
function from the example , which is responsible for drawing the user primitive, so that the color of the elements is read from the specified properties of the document: 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); }
Source: https://habr.com/ru/post/226563/
All Articles