private static IList <GeometryObject> GenerateTessellatedGeo(DesignScriptEntity shapeReference, ElementId materialId) { IList <GeometryObject> tessellatedShape = null; if (shapeReference is Autodesk.DesignScript.Geometry.Mesh) { tessellatedShape = (shapeReference as Autodesk.DesignScript.Geometry.Mesh).ToRevitType( TessellatedShapeBuilderTarget.Mesh, TessellatedShapeBuilderFallback.Salvage, MaterialId: materialId); } else if (shapeReference is Autodesk.DesignScript.Geometry.Surface) { tessellatedShape = (shapeReference as Autodesk.DesignScript.Geometry.Surface).ToRevitType( TessellatedShapeBuilderTarget.AnyGeometry, TessellatedShapeBuilderFallback.Mesh, MaterialId: materialId); } else if (shapeReference is Autodesk.DesignScript.Geometry.Solid) { tessellatedShape = (shapeReference as Autodesk.DesignScript.Geometry.Solid).ToRevitType( TessellatedShapeBuilderTarget.AnyGeometry, TessellatedShapeBuilderFallback.Mesh, MaterialId: materialId); } if (tessellatedShape == null) { throw new ArgumentException(Revit.Properties.Resources.DirectShapeInvalidArgument); } return(tessellatedShape); }
public static T SetSpeckleProperties <T>(this DesignScriptEntity geometry, Dictionary <string, object> properties) { if (properties != null) { geometry.Tags.AddTag(speckleKey, properties.ToNativeX()); } return((T)Convert.ChangeType(geometry, typeof(T))); }
/// <summary> /// Initialize a DirectShape element /// </summary> private void InitDirectShape(DesignScriptEntity shapeReference, string shapeName, Category category, Material material) { //Phase 1 - Check to see if a DirectShape exists in trace and should be rebound var oldShape = ElementBinder.GetElementAndTraceData <Autodesk.Revit.DB.DirectShape, DirectShapeState>(Document); //There was a oldDirectShape, rebind to that if (oldShape != null) { //set the directshape element this.InternalSetDirectShape(oldShape.Item1); //if the cateogryID has changed, we cannot continue to rebind and instead //will make a new directShape if (category.Id == this.InternalElement.Category.Id.IntegerValue) { //set the shape geometry of the directshape, this method passes in the actual input geo //and checks the directShapeState object at the elementId key in the input geo tags dictionary. //this check is used to determine if the geometry should be updated, this is done by checking //the sync guid in the DirectShapeState (a guid that is generated when geometry changes on //the revit element. //we also check the material, if it's different than the currently assigned material //then we need to rebuild the geo so that a new material is applied // this.InternalSetShape(shapeReference, new ElementId(material.Id), oldShape.Item2.syncId); this.InternalSetName(shapeReference, shapeName, material, category); return; } } //Phase 2- There was no existing shape, create one TransactionManager.Instance.EnsureInTransaction(Document); Autodesk.Revit.DB.DirectShape ds; //generate the geometry correctly depending on the type of geo var tessellatedShape = GenerateTessellatedGeo(shapeReference, new ElementId(material.Id)); //actually construct the directshape revit element ds = NewDirectShape(tessellatedShape, Document, new ElementId(category.Id), DirectShape.DYNAMO_DIRECTSHAPE_APP_GUID.ToString(), shapeName); InternalSetDirectShape(ds); InternalSetName(shapeReference, shapeName, material, category); //generate a new syncId for trace var traceData = new DirectShapeState(this, Guid.NewGuid().ToString(), new ElementId(material.Id)); //add the elementID:tracedata to the tags dictionary on the real protogeometry input shapeReference.Tags.AddTag(this.InternalElementId.ToString(), traceData); TransactionManager.Instance.TransactionTaskDone(); ElementBinder.SetRawDataForTrace(traceData); }
/// <summary> /// Copies props from a design script entity to a speckle object. /// </summary> /// <param name="to"></param> /// <param name="from"></param> public void CopyProperties(Base to, DesignScriptEntity from) { var dict = from.GetSpeckleProperties(); foreach (var kvp in dict) { to[kvp.Key] = kvp.Value; } }
public static Dictionary <string, object> GetSpeckleProperties(this DesignScriptEntity geometry) { var userData = geometry.Tags.LookupTag(speckleKey) as DesignScript.Builtin.Dictionary; if (userData == null) { return(new Dictionary <string, object>()); } return(userData.ToSpeckleX()); ; }
/// <summary> /// Sets the internalDirectShape to have a new name /// if this method detects the default string it generates a more specific name /// </summary> private void InternalSetName(DesignScriptEntity shapeReference, string name, Material material, Category category) { if (name == DEFAULT_NAME) { name = string.Format("DirectShape" + "(Geometry = {0}, Category = {1}, Material ={2}.)", shapeReference.ToString(), category != null ? category.Name : "", material != null ? material.Name : ""); } if (name != this.InternalDirectShape.Name) { TransactionManager.Instance.EnsureInTransaction(Document); this.InternalDirectShape.SetName(name); TransactionManager.Instance.TransactionTaskDone(); } }
/// <summary> /// Sets the internalDirectShape's shape to point to some geometry, /// this method also generates tessellated geometry from the protogeometry object /// and sets the material of the generated Revit faces /// </summary> private void InternalSetShape(DesignScriptEntity shapeReference, ElementId materialId, string currentSyncId) { //if the elementID for the current directShape revitElement exists on the input Geometry AND //the value stored at that key is equal to the materialId we're trying to set AND //the previousState's syncId (in the tags dictionary) equals the current syncId (from trace) //then we do not need to regenerate //first lookup the state on the input geometry var previousState = shapeReference.Tags.LookupTag(this.InternalElementId.ToString()) as DirectShapeState; //then compare values if (previousState != null && previousState.materialId == materialId.IntegerValue && previousState.syncId == currentSyncId) { return; } TransactionManager.Instance.EnsureInTransaction(Document); var tessellatedShape = GenerateTessellatedGeo(shapeReference, materialId); this.InternalDirectShape.SetShape(tessellatedShape); //update the value in trace, since we had to modify the geometry we need a new syncId var updatedTraceData = new DirectShapeState(this, Guid.NewGuid().ToString(), materialId); ElementBinder.SetRawDataForTrace(updatedTraceData); //place new values in the tags dict if (shapeReference.Tags.LookupTag(this.InternalElementId.ToString()) == null) { shapeReference.Tags.AddTag(this.InternalElementId.ToString(), updatedTraceData); } else { var storedState = shapeReference.Tags.LookupTag(this.InternalElementId.ToString()) as DirectShapeState; storedState.syncId = updatedTraceData.syncId; storedState.materialId = updatedTraceData.materialId; storedState.IntID = updatedTraceData.IntID; storedState.StringID = updatedTraceData.StringID; } TransactionManager.Instance.TransactionTaskDone(); }
public static Dictionary Get(object geometry) { if (geometry == null) { throw new ArgumentNullException("geometry"); } if (!(geometry is DesignScriptEntity)) { throw new ArgumentException(String.Format(typeException, geometry.GetType()), "geometry"); } DesignScriptEntity dsEntity = geometry as DesignScriptEntity; var dict = (Dictionary)dsEntity.Tags.LookupTag(speckleKey); if (dict == null) { return(null); } else { return(dict); } }
/// <summary> /// Internal Constructor for a new DirectShape /// </summary> protected DirectShape(DesignScriptEntity shapeReference, string shapename, Category category, Material material) { SafeInit(() => InitDirectShape(shapeReference, shapename, category, material)); }
/// <summary> /// /// </summary> /// <param name="obj"></param> /// <returns></returns> protected override bool Equals(DesignScriptEntity obj) { var pln = obj as Plane; if (pln == null) { return false; } return Origin.Equals(pln.Origin) && Normal.Equals(pln.Normal); }
private static IList <GeometryObject> GenerateTessellatedGeo(DesignScriptEntity shapeReference, ElementId materialId) { IList <GeometryObject> tessellatedShape = null; if (shapeReference is Autodesk.DesignScript.Geometry.Mesh) { tessellatedShape = (shapeReference as Autodesk.DesignScript.Geometry.Mesh).ToRevitType( TessellatedShapeBuilderTarget.Mesh, TessellatedShapeBuilderFallback.Salvage, MaterialId: materialId); } else if (shapeReference is Autodesk.DesignScript.Geometry.Surface) { // Defer to tessellated shape if BRep shape for some reason fails GeometryObject geometry = null; try { geometry = DynamoToRevitBRep.ToRevitType(shapeReference as Autodesk.DesignScript.Geometry.Surface, true, materialId); } catch { } if (geometry != null) { tessellatedShape = new List <GeometryObject>() { geometry }; } else { tessellatedShape = (shapeReference as Autodesk.DesignScript.Geometry.Surface).ToRevitType(TessellatedShapeBuilderTarget.AnyGeometry, TessellatedShapeBuilderFallback.Mesh, MaterialId: materialId); } } else if (shapeReference is Autodesk.DesignScript.Geometry.Solid) { // Defer to tessellated shape if BRep shape for some reason fails GeometryObject geometry = null; try { geometry = DynamoToRevitBRep.ToRevitType(shapeReference as Autodesk.DesignScript.Geometry.Solid, true, materialId); } catch { } if (geometry != null) { tessellatedShape = new List <GeometryObject>() { geometry }; } else { tessellatedShape = (shapeReference as Autodesk.DesignScript.Geometry.Solid).ToRevitType(TessellatedShapeBuilderTarget.AnyGeometry, TessellatedShapeBuilderFallback.Mesh, MaterialId: materialId); } } if (tessellatedShape == null) { throw new ArgumentException(Revit.Properties.Resources.DirectShapeInvalidArgument); } return(tessellatedShape); }
/// <summary> /// /// </summary> /// <param name="obj"></param> /// <returns></returns> protected override bool Equals(DesignScriptEntity obj) { if (base.Equals(obj)) { return true; } var pt = obj as Point; if (pt == null) { return false; } else { return GeometryExtension.Equals(X, pt.X) && GeometryExtension.Equals(Y, pt.Y) && GeometryExtension.Equals(Z, pt.Z); } }
/// <summary> /// /// </summary> /// <param name="obj"></param> /// <returns></returns> protected override bool Equals(DesignScriptEntity obj) { if (base.Equals(obj)) { return true; } var arc = obj as DSArc; if (arc == null) { return false; } return CenterPoint.Equals(arc.CenterPoint) && Normal.Equals(arc.Normal) && DSGeometryExtension.Equals(Radius, arc.Radius); }
/// <summary> /// /// </summary> /// <param name="obj"></param> /// <returns></returns> protected override bool Equals(DesignScriptEntity obj) { if (base.Equals(obj)) { return true; } var otherLine = obj as Line; if (otherLine == null) { return false; } return StartPoint.Equals(otherLine.StartPoint) && EndPoint.Equals(otherLine.EndPoint); }
/// <summary> /// /// </summary> /// <param name="obj"></param> /// <returns></returns> protected override bool Equals(DesignScriptEntity obj) { if (base.Equals(obj)) { return true; } var other = obj as DSCircle; if (obj == null || other == null) { return false; } return CenterPoint.Equals(other.CenterPoint) && DSGeometryExtension.Equals(Radius, other.Radius) && Normal.Equals(other.Normal); }
/// <summary> /// /// </summary> /// <param name="obj"></param> /// <returns></returns> protected override bool Equals(DesignScriptEntity obj) { var otherCS = obj as CoordinateSystem; if( null == otherCS) { return false; } return CSEntity.IsEqualTo(otherCS.CSEntity); }
public GeometryGraphicItem(DesignScriptEntity entity) { mEntity = entity; }