/// <summary> /// creates a DirectShape instance of which shape is Sphere. /// Sphere is defined by its center and radius. /// </summary> /// <param name="document">The Revit document where the instance to be drawn</param> /// <param name="name">The name of this instance</param> /// <param name="center">Position of the center</param> /// <param name="radius">Radius of the circle</param> /// <param name="line_color">Outline color of Circle</param> /// <param name="surface_transparency">Surface transparency; ranged from 0 (transparent) to 100 (opaque)</param> public DirectSphere(Document document, string name, XYZ center, double radius, Color line_color, int surface_transparency) : base(document, name) { m_shape_type = ShapeTypes.Sphere; Center = center; Radius = radius; XYZ top = center + radius * XYZ.BasisZ; XYZ bottom = center - radius * XYZ.BasisZ; XYZ right = center + radius * XYZ.BasisX; Frame frame = new Frame(center, XYZ.BasisX, XYZ.BasisY, XYZ.BasisZ); List <Curve> profile = new List <Curve>(); profile.Add(Line.CreateBound(top, bottom)); profile.Add(Arc.Create(bottom, top, right)); CurveLoop curve_loop = CurveLoop.Create(profile); SolidOptions options = new SolidOptions(ElementId.InvalidElementId, ElementId.InvalidElementId); if (Frame.CanDefineRevitGeometry(frame) == true) { Solid sphere = GeometryCreationUtilities.CreateRevolvedGeometry(frame, new CurveLoop[] { curve_loop }, 0, 2 * Math.PI, options); using (Transaction t = new Transaction(document, "Create sphere direct shape.")) { t.Start(); DirectShape shape = DirectShape.CreateElement(document, new ElementId(BuiltInCategory.OST_GenericModel)); shape.SetShape(new GeometryObject[] { sphere }); shape.SetName(name); m_element_id = shape.Id; document.ActiveView.SetElementOverrides(shape.Id, new OverrideGraphicSettings().SetProjectionLineColor(line_color).SetSurfaceTransparency(surface_transparency)); t.Commit(); } } }
public static bool CreateAreaSolid(Document doc, AreaProperties ap, out MassProperties createdMass) { bool created = false; createdMass = null; try { string appGuid = doc.Application.ActiveAddInId.GetGUID().ToString(); if (null != ap.Linked3dMass) { //delete existing mass first MassProperties existingMass = ap.Linked3dMass; doc.Delete(new ElementId(existingMass.MassId)); } IList <GeometryObject> areaGeometries = new List <GeometryObject>(); if (ap.AreaProfile.Count > 0) { XYZ extrusionDir = new XYZ(0, 0, 1); Solid areaSolid = GeometryCreationUtilities.CreateExtrusionGeometry(ap.AreaProfile, extrusionDir, ap.UserHeight); if (null != areaSolid) { areaGeometries.Add(areaSolid); } } #if RELEASE2015 || RELEASE2016 DirectShape createdShape = DirectShape.CreateElement(doc, new ElementId(massCategory), appGuid, ap.AreaId.ToString()); #else DirectShape createdShape = DirectShape.CreateElement(doc, new ElementId(massCategory)); createdShape.ApplicationId = appGuid; createdShape.ApplicationDataId = ap.AreaId.ToString(); #endif #if RELEASE2016 DirectShapeOptions options = createdShape.GetOptions(); options.ReferencingOption = DirectShapeReferencingOption.Referenceable; createdShape.SetOptions(options); #endif createdShape.SetShape(areaGeometries); createdShape.SetName(ap.AreaName); Element massElement = doc.GetElement(createdShape.Id); if (null != massElement) { createdMass = new MassProperties(massElement); createdMass.SetHostInfo(ap.AreaUniqueId, SourceType.Areas, ap.AreaCenterPoint, ap.UserHeight); bool stored = MassDataStorageUtil.SetLinkedHostInfo(massElement, SourceType.Areas.ToString(), ap.AreaUniqueId, ap.AreaCenterPoint, ap.UserHeight); created = true; } } catch (Exception ex) { MessageBox.Show("Failed to create area solid.\n" + ex.Message, "Create Area Solid", MessageBoxButtons.OK, MessageBoxIcon.Warning); } return(created); }
/// <summary> /// creates a DirectShape instance of which shape is a part of a torus (like elbow joint pipe). /// Torus is defined by center, axis, tube radius, and mean radius (the distance between center and tube center). /// The tube_begin and tube_end defines the angle between the two edges of the piece. /// </summary> /// <param name="document">The Revit document where the instance to be drawn</param> /// <param name="name">The name of this instance</param> /// <param name="center">Position of center of the torus' hole</param> /// <param name="axis">Vector passing through the center</param> /// <param name="mean_radius">The distance between torus center and its tube center</param> /// <param name="tube_radius">Radius of tube</param> /// <param name="tube_begin">The vector pointing to one of the torus' edge from its center</param> /// <param name="torus_angle">The angle between the tube begin and end</param> /// <param name="line_color">Outline color of the torus</param> /// <param name="surface_transparency">Surface transparency; ranged from 0 (transparent) to 100 (opaque)</param> public DirectTorus(Document document, string name, XYZ center, XYZ axis, double mean_radius, double tube_radius, XYZ tube_begin, double torus_angle, Color line_color, int surface_transparency) : base(document, name) { m_shape_type = ShapeTypes.Torus; Center = center; Axis = axis; MeanRadius = mean_radius; TubeRadius = tube_radius; HasAnElbow = true; TubeBegin = tube_begin; TubeAngle = torus_angle; XYZ tilting_axis = XYZ.BasisZ.CrossProduct(axis); double tilting_angle = FindSurfaceRevitPluginUtils.GetPositiveAngleBetween(XYZ.BasisZ, axis, tilting_axis); bool no_need_to_tilt = tilting_axis.IsAlmostEqualTo(XYZ.Zero); Transform tilting_torus = no_need_to_tilt ? Transform.Identity : Transform.CreateRotation(tilting_axis, tilting_angle); XYZ tilted_basis_x = tilting_torus.OfVector(XYZ.BasisX); // model space coordinates Frame frame = new Frame(XYZ.Zero, XYZ.BasisX, XYZ.BasisY, XYZ.BasisZ); XYZ model_tube_center = XYZ.BasisX * mean_radius; XYZ model_tube_top = model_tube_center + tube_radius * XYZ.BasisZ; XYZ model_tube_bottom = model_tube_center - tube_radius * XYZ.BasisZ; XYZ model_tube_outer = model_tube_center + tube_radius * XYZ.BasisX; XYZ model_tube_inner = model_tube_center - tube_radius * XYZ.BasisX; List <Curve> tube_circle = new List <Curve>(); tube_circle.Add(Arc.Create(model_tube_top, model_tube_bottom, model_tube_inner)); tube_circle.Add(Arc.Create(model_tube_bottom, model_tube_top, model_tube_outer)); CurveLoop curve_loop = CurveLoop.Create(tube_circle); SolidOptions options = new SolidOptions(ElementId.InvalidElementId, ElementId.InvalidElementId); if (Frame.CanDefineRevitGeometry(frame)) { Solid torus = GeometryCreationUtilities.CreateRevolvedGeometry(frame, new CurveLoop[] { curve_loop }, 0, torus_angle, options); using (Transaction t = new Transaction(document, "Create torus direct shape.")) { t.Start(); DirectShape shape = DirectShape.CreateElement(document, new ElementId(BuiltInCategory.OST_GenericModel)); shape.SetShape(new GeometryObject[] { torus }); shape.SetName(name); m_element_id = shape.Id; if (no_need_to_tilt == false) { shape.Location.Rotate(Line.CreateUnbound(XYZ.Zero, tilting_axis), tilting_angle); } shape.Location.Rotate(Line.CreateUnbound(XYZ.Zero, axis), FindSurfaceRevitPluginUtils.GetPositiveAngleBetween(tilted_basis_x, tube_begin, axis)); shape.Location.Move(center); document.ActiveView.SetElementOverrides(shape.Id, new OverrideGraphicSettings().SetProjectionLineColor(line_color).SetSurfaceTransparency(surface_transparency)); t.Commit(); } } }
/// <summary> /// creates a DirectShape instance of which shape is Plane. /// Plane is defined by four vertices (top-left, top-right, bottom-left, bottom-right) on the plane. /// (Actually, it is a rectangle as you already know.) /// </summary> /// <remarks>The Plane has very small thickness (0.0039) since there is no way to create a plane with no thickness using DirectShape.</remarks> /// <param name="document">The Revit document where the instance to be drawn</param> /// <param name="name">The name of this instance</param> /// <param name="top_left">Position of the top-left vertex</param> /// <param name="top_right">Position of the top-right vertex</param> /// <param name="bottom_left">Position of the bottom-left vertex</param> /// <param name="bottom_right">Position of the bottom-right vertex</param> /// <param name="line_color">Outline color of Plane</param> /// <param name="surface_transparency">Surface transparency; ranged from 0 (transparent) to 100 (opaque)</param> public DirectPlane(Document document, string name, XYZ top_left, XYZ top_right, XYZ bottom_left, XYZ bottom_right, Color line_color, int surface_transparency) : base(document, name) { m_shape_type = ShapeTypes.Plane; TopLeft = top_left; TopRight = top_right; BottomLeft = bottom_left; BottomRight = bottom_right; XYZ rotation_axis, translation_offset; double rotation_angle; XYZ tl, tr; XYZ bl, br; // We'll rotates and translates the plane transformed to be axis-aligned, because GeometryCreationUtilities.CreateSweptGeometry may fail to define a plane due to the precision issue. GetAxisAlignedPlane( top_left, top_right, bottom_left, bottom_right, out tl, out tr, out bl, out br, out rotation_axis, out rotation_angle, out translation_offset); Frame frame = new Frame(XYZ.Zero, XYZ.BasisX, XYZ.BasisY, XYZ.BasisZ); List <Curve> profile = new List <Curve>(); profile.Add(Line.CreateBound(tl, bl)); profile.Add(Line.CreateBound(bl, br)); profile.Add(Line.CreateBound(br, tr)); profile.Add(Line.CreateBound(tr, tl)); List <Curve> swept_profile = new List <Curve>(); swept_profile.Add(Line.CreateBound(XYZ.Zero, 0.0039 * XYZ.BasisZ)); CurveLoop curve_loop = CurveLoop.Create(profile); CurveLoop sweep_path = CurveLoop.Create(swept_profile); SolidOptions options = new SolidOptions(ElementId.InvalidElementId, ElementId.InvalidElementId); if (Frame.CanDefineRevitGeometry(frame) == true) { Solid thin_box = GeometryCreationUtilities.CreateSweptGeometry(sweep_path, 0, 0, new CurveLoop[] { curve_loop }, options); using (Transaction t = new Transaction(document, "Create plane direct shape")) { t.Start(); DirectShape shape = DirectShape.CreateElement(document, new ElementId(BuiltInCategory.OST_GenericModel)); shape.SetShape(new GeometryObject[] { thin_box }); shape.SetName(name); m_element_id = shape.Id; shape.Location.Rotate(Line.CreateUnbound(XYZ.Zero, rotation_axis), -rotation_angle); shape.Location.Move(-translation_offset); document.ActiveView.SetElementOverrides(shape.Id, new OverrideGraphicSettings().SetProjectionLineColor(line_color).SetSurfaceTransparency(surface_transparency)); t.Commit(); } } }
public static bool CreateFloorFace(Document doc, FloorProperties fp, out MassProperties createdMass) { bool created = false; createdMass = null; try { string appGuid = doc.Application.ActiveAddInId.GetGUID().ToString(); if (null != fp.Linked2dMass) { //delete existing mass first MassProperties existingMass = fp.Linked2dMass; doc.Delete(new ElementId(existingMass.MassId)); } IList <GeometryObject> floorGeometries = GetGeometryObjectsFromFace(fp.TopFace); #if RELEASE2015 || RELEASE2016 DirectShape createdShape = DirectShape.CreateElement(doc, new ElementId(massCategory), appGuid, fp.FloorId.ToString()); #else DirectShape createdShape = DirectShape.CreateElement(doc, new ElementId(massCategory)); createdShape.ApplicationId = appGuid; createdShape.ApplicationDataId = fp.FloorId.ToString(); #endif #if RELEASE2016 DirectShapeOptions options = createdShape.GetOptions(); options.ReferencingOption = DirectShapeReferencingOption.Referenceable; createdShape.SetOptions(options); #endif createdShape.SetShape(floorGeometries); createdShape.SetName(fp.FloorName); Element massElement = doc.GetElement(createdShape.Id); if (null != massElement) { createdMass = new MassProperties(massElement); createdMass.MassElementType = MassType.MASS2D; createdMass.SetHostInfo(fp.FloorUniqueId, SourceType.Floors, fp.FloorSolidCentroid, 0); bool stored = MassDataStorageUtil.SetLinkedHostInfo(massElement, SourceType.Floors.ToString(), fp.FloorUniqueId, fp.FloorSolidCentroid, 0); created = true; } } catch (Exception ex) { MessageBox.Show("Failed to create room solid.\n" + ex.Message, "Create Room Solid", MessageBoxButtons.OK, MessageBoxIcon.Warning); } return(created); }
/// <summary> /// creates a DirectShape instance of which shape is Torus. /// Torus is defined by center, axis, tube radius, and mean radius (the distance between center and tube center). /// </summary> /// <param name="document">The Revit document where the instance to be drawn</param> /// <param name="name">The name of this instance</param> /// <param name="center">Position of center of the torus' hole</param> /// <param name="axis">Vector passing through the center</param> /// <param name="mean_radius">The distance between the center and tube center</param> /// <param name="tube_radius">Radius of tube</param> /// <param name="line_color">Outline color</param> /// <param name="surface_transparency">Surface transparency; ranged from 0 (transparent) to 100 (opaque)</param> public DirectTorus(Document document, string name, XYZ center, XYZ axis, double mean_radius, double tube_radius, Color line_color, int surface_transparency) : base(document, name) { m_shape_type = ShapeTypes.Torus; Center = center; Axis = axis; MeanRadius = mean_radius; TubeRadius = tube_radius; HasAnElbow = false; TubeBegin = new XYZ(); TubeAngle = 0.0; XYZ axis_norm = axis.Normalize(); XYZ minor_center = ((XYZ.BasisX.CrossProduct(axis_norm).GetLength() < Double.Epsilon) ? XYZ.BasisY : XYZ.BasisX).CrossProduct(axis_norm); minor_center = center + minor_center.Normalize() * mean_radius; XYZ basis_z = axis.Normalize(); XYZ basis_x = (minor_center - center).Normalize(); XYZ basis_y = basis_z.CrossProduct(basis_x).Normalize(); Frame frame = new Frame(center, basis_x, basis_y, basis_z); // model space coordinates XYZ near = minor_center - tube_radius * basis_x; XYZ far = minor_center + tube_radius * basis_x; XYZ back = minor_center + tube_radius * basis_z; XYZ front = minor_center - tube_radius * basis_z; List <Curve> profile = new List <Curve>(); profile.Add(Arc.Create(near, far, front)); profile.Add(Arc.Create(far, near, back)); CurveLoop curve_loop = CurveLoop.Create(profile); SolidOptions options = new SolidOptions(ElementId.InvalidElementId, ElementId.InvalidElementId); if (Frame.CanDefineRevitGeometry(frame) == true) { Solid torus = GeometryCreationUtilities.CreateRevolvedGeometry(frame, new CurveLoop[] { curve_loop }, 0, 2 * Math.PI, options); using (Transaction t = new Transaction(document, "Create torus direct shape")) { t.Start(); DirectShape shape = DirectShape.CreateElement(document, new ElementId(BuiltInCategory.OST_GenericModel)); shape.SetShape(new GeometryObject[] { torus }); shape.SetName(name); document.ActiveView.SetElementOverrides(shape.Id, new OverrideGraphicSettings().SetProjectionLineColor(line_color).SetSurfaceTransparency(surface_transparency)); t.Commit(); } } }
/// <summary> /// creates a DirectShape instance of which shape is Cylinder. /// Cylinder is defined by two circles (top, bottom) with the same radius. /// </summary> /// <param name="document">The Revit document where the instance to be drawn</param> /// <param name="name">The name of this instance</param> /// <param name="doc">The Revit document where the instance to be drawn</param> /// <param name="top">Position of center of the top circle</param> /// <param name="bottom">Position of center of the bottom circle</param> /// <param name="radius">Radius of the circles</param> /// <param name="line_color">Outline color of Cylinder</param> /// <param name="surface_transparency">Surface transparency; ranged from 0 (transparent) to 100 (opaque)</param> public DirectCylinder(Document document, string name, XYZ top, XYZ bottom, double radius, Color line_color, int surface_transparency) : base(document, name) { m_shape_type = ShapeTypes.Cylinder; Top = top; Bottom = bottom; Radius = radius; // defines a reference frame of which origin is at the center of the cylinder and z-axis is passing through the centers of its top and bottom. XYZ center = (top + bottom) / 2; XYZ basis_z = (top - bottom).Normalize(); XYZ basis_x = XYZ.BasisY.CrossProduct(basis_z).Normalize(); XYZ basis_y = basis_z.CrossProduct(basis_x).Normalize(); Frame frame = new Frame(center, basis_x, basis_y, basis_z); XYZ bottom_left = bottom - radius * basis_x; XYZ bottom_right = bottom + radius * basis_x; XYZ bottom_front = bottom - radius * basis_y; XYZ bottom_back = bottom + radius * basis_y; // creates a profile that is a cross section of a circle (the cylinder will be made by sweeping through the z-axis). List <Curve> profile = new List <Curve>(); profile.Add(Arc.Create(bottom_left, bottom_right, bottom_back)); profile.Add(Arc.Create(bottom_right, bottom_left, bottom_front)); List <Curve> swept_profile = new List <Curve>(); swept_profile.Add(Line.CreateBound(bottom, top)); CurveLoop curve_loop = CurveLoop.Create(profile); CurveLoop sweep_path = CurveLoop.Create(swept_profile); SolidOptions options = new SolidOptions(ElementId.InvalidElementId, ElementId.InvalidElementId); if (Frame.CanDefineRevitGeometry(frame) == true) { Solid cylinder = GeometryCreationUtilities.CreateSweptGeometry(sweep_path, 0, 0, new CurveLoop[] { curve_loop }, options); using (Transaction t = new Transaction(document, "Create cylinder direct shape")) { t.Start(); DirectShape shape = DirectShape.CreateElement(document, new ElementId(BuiltInCategory.OST_GenericModel)); shape.SetShape(new GeometryObject[] { cylinder }); shape.SetName(name); m_element_id = shape.Id; document.ActiveView.SetElementOverrides(shape.Id, new OverrideGraphicSettings().SetProjectionLineColor(line_color).SetSurfaceTransparency(surface_transparency)); t.Commit(); } } }
public static bool CreateRoomFace(Document doc, RoomProperties rp, out MassProperties createdMass) { bool created = false; createdMass = null; try { string appGuid = doc.Application.ActiveAddInId.GetGUID().ToString(); if (null != rp.Linked2dMass) { //delete existing mass first MassProperties existingMass = rp.Linked2dMass; doc.Delete(new ElementId(existingMass.MassId)); } if (null != rp.BottomFace) { IList <GeometryObject> roomGeometries = GetGeometryObjectsFromFace(rp.BottomFace); #if RELEASE2015 || RELEASE2016 DirectShape createdShape = DirectShape.CreateElement(doc, new ElementId(massCategory), appGuid, rp.RoomId.ToString()); #else DirectShape createdShape = DirectShape.CreateElement(doc, new ElementId(massCategory)); createdShape.ApplicationId = appGuid; createdShape.ApplicationDataId = rp.RoomId.ToString(); #endif createdShape.SetShape(roomGeometries); createdShape.SetName(rp.RoomName); Element massElement = doc.GetElement(createdShape.Id); if (null != massElement) { createdMass = new MassProperties(massElement); createdMass.MassElementType = MassType.MASS2D; createdMass.SetHostInfo(rp.RoomUniqueId, SourceType.Rooms, rp.RoomSolidCentroid, 0); bool stored = MassDataStorageUtil.SetLinkedHostInfo(massElement, SourceType.Rooms.ToString(), rp.RoomUniqueId, rp.RoomSolidCentroid, 0); created = true; } } } catch (Exception ex) { MessageBox.Show("Failed to create room face.\n" + ex.Message, "Create Room Face", MessageBoxButtons.OK, MessageBoxIcon.Warning); } return(created); }
/// <summary> /// Creates a Direct Shape object. /// </summary> /// <param name="doc">Document</param> /// <param name="element">Element to convert.</param> /// <returns></returns> private static DirectShapeInfo CreateDirectShape(Document doc, Element element) { DirectShapeInfo shapeInfo = null; var isFamilyInstance = false; try { if (element.GetType() == typeof(FamilyInstance)) { isFamilyInstance = true; } if (element.GroupId != ElementId.InvalidElementId) { var group = doc.GetElement(element.GroupId) as Group; group?.UngroupMembers(); } if (isFamilyInstance) { var subComponents = ((FamilyInstance)element).GetSubComponentIds(); foreach (var subId in subComponents) { var subComponent = doc.GetElement(subId); if (subComponent != null) { CreateDirectShape(doc, subComponent); //TODO: same as above. It's worth creating them, but they are not returned. } } } var geoOptions = new Options(); var geoElement = element.get_Geometry(geoOptions); if (null != geoElement) { try { DirectShape directShape = null; #if RELEASE2015 || RELEASE2016 var shapeTypeId = ElementId.InvalidElementId; var appDataGUID = element.Id.IntegerValue.ToString(); #endif var shapeGeometries = FindElementGeometry(geoElement); if (shapeGeometries.Count > 0) { #if RELEASE2015 || RELEASE2016 directShape = DirectShape.CreateElement(doc, element.Category.Id, appDataGUID, appDataGUID); #else directShape = DirectShape.CreateElement(doc, element.Category.Id); #endif directShape.SetShape(shapeGeometries); } if (null != directShape) { directShape.SetName(element.Name); #if RELEASE2016 || RELEASE2017 || RELEASE2018 || RELEASE2019 var dsOptions = directShape.GetOptions(); dsOptions.ReferencingOption = DirectShapeReferencingOption.Referenceable; directShape.SetOptions(dsOptions); #endif shapeInfo = new DirectShapeInfo(directShape.Id, element.Id); } } catch (Exception ex) { Log.AppendLog(LogMessageType.EXCEPTION, "Cannot Set Geometry of DirectShape-" + ex.Message + element.Id); } } } catch (Exception ex) { Log.AppendLog(LogMessageType.EXCEPTION, "Cannot Create DirectShape-" + ex.Message + element.Id); } return(shapeInfo); }