diff --git a/ConnectorAutocadCivil/ConnectorAutocadCivil/UI/ConnectorBindingsAutocadCivil.cs b/ConnectorAutocadCivil/ConnectorAutocadCivil/UI/ConnectorBindingsAutocadCivil.cs index 231d53bbe4..84d451c316 100644 --- a/ConnectorAutocadCivil/ConnectorAutocadCivil/UI/ConnectorBindingsAutocadCivil.cs +++ b/ConnectorAutocadCivil/ConnectorAutocadCivil/UI/ConnectorBindingsAutocadCivil.cs @@ -324,6 +324,17 @@ private void ConvertReceiveCommit(Base commitObject, ISpeckleConverter converter if (display == null) display = obj[@"renderMaterial"] as Base; if (display != null) Utils.SetStyle(display, convertedEntity, lineTypeDictionary); + // add property sets if this is Civil3D +#if CIVIL2021 || CIVIL2022 || CIVIL2023 + if (obj["propertySets"] is IReadOnlyList list) + { + var propertySets = new List>(); + foreach (var listObj in list) + propertySets.Add(listObj as Dictionary); + convertedEntity.SetPropertySets(Doc, propertySets); + } +#endif + tr.TransactionManager.QueueForGraphicsFlush(); } else diff --git a/ConnectorAutocadCivil/ConnectorAutocadCivil/Utils.cs b/ConnectorAutocadCivil/ConnectorAutocadCivil/Utils.cs index c389df0e4e..765983d4b9 100644 --- a/ConnectorAutocadCivil/ConnectorAutocadCivil/Utils.cs +++ b/ConnectorAutocadCivil/ConnectorAutocadCivil/Utils.cs @@ -193,6 +193,79 @@ public static bool Visible(this DBObject obj) } #if CIVIL2021 || CIVIL2022 || CIVIL2023 + private static Autodesk.Aec.PropertyData.DataType? GetPropertySetType(object prop) + { + switch (prop) + { + case IEnumerable _: + case IEnumerable _: + case IEnumerable _: + case IEnumerable _: + return Autodesk.Aec.PropertyData.DataType.List; + + case string _: + return Autodesk.Aec.PropertyData.DataType.Text; + case int _: + return Autodesk.Aec.PropertyData.DataType.Integer; + case double _: + return Autodesk.Aec.PropertyData.DataType.Real; + case bool _: + return Autodesk.Aec.PropertyData.DataType.TrueFalse; + + default: + return null; + } + } + + public static void SetPropertySets(this Entity entity, Document doc, List> propertySetDicts) + { + // create a dictionary for property sets for this object + var name = $"Speckle {entity.Handle} Property Set"; + int count = 0; + foreach (var propertySetDict in propertySetDicts) + { + // create the property set definition for this set. + var propSetDef = new PropertySetDefinition(); + propSetDef.SetToStandard(doc.Database); + propSetDef.SubSetDatabaseDefaults(doc.Database); + var propSetDefName = name += $" - {count}"; + propSetDef.Description = "Property Set Definition added with Speckle"; + propSetDef.AppliesToAll = true; + + // Create the definition for each property + foreach (var entry in propertySetDict) + { + var propDef = new PropertyDefinition(); + propDef.SetToStandard(doc.Database); + propDef.SubSetDatabaseDefaults(doc.Database); + propDef.Name = entry.Key; + var dataType = GetPropertySetType(entry.Value); + if (dataType != null) + propDef.DataType = (Autodesk.Aec.PropertyData.DataType)dataType; + propDef.DefaultData = entry.Value; + propSetDef.Definitions.Add(propDef); + } + + // add the property sets to the object + try + { + // add property set to the database + // todo: add logging if the property set couldnt be added because a def already exists + using (Transaction tr = doc.Database.TransactionManager.StartTransaction()) + { + var dictPropSetDef = new DictionaryPropertySetDefinitions(doc.Database); + dictPropSetDef.AddNewRecord(propSetDefName, propSetDef); + tr.AddNewlyCreatedDBObject(propSetDef, true); + + entity.UpgradeOpen(); + PropertyDataServices.AddPropertySet(entity, propSetDef.ObjectId); + tr.Commit(); + } + } + catch { } + } + } + /// /// Get the property sets of DBObject ///