Skip to content

Commit

Permalink
feat(civil3d): adds propertysets on receive (#1565)
Browse files Browse the repository at this point in the history
* adds method for creating and attaching property sets on receive

* fixes memory violation error
  • Loading branch information
clairekuang authored Aug 31, 2022
1 parent 45a5ba6 commit 66ebc0f
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<object> list)
{
var propertySets = new List<Dictionary<string, object>>();
foreach (var listObj in list)
propertySets.Add(listObj as Dictionary<string, object>);
convertedEntity.SetPropertySets(Doc, propertySets);
}
#endif

tr.TransactionManager.QueueForGraphicsFlush();
}
else
Expand Down
73 changes: 73 additions & 0 deletions ConnectorAutocadCivil/ConnectorAutocadCivil/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> _:
case IEnumerable<int> _:
case IEnumerable<double> _:
case IEnumerable<bool> _:
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<Dictionary<string, object>> 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 { }
}
}

/// <summary>
/// Get the property sets of DBObject
/// </summary>
Expand Down

0 comments on commit 66ebc0f

Please sign in to comment.