public static DirectShape CreateDirectShape( Document doc, Solid transientSolid, string dsName) { ElementId catId = new ElementId( BuiltInCategory.OST_GenericModel); AddInId addInId = doc.Application.ActiveAddInId; DirectShape ds = DirectShape.CreateElement(doc, catId, addInId.GetGUID().ToString(), ""); if (ds.IsValidGeometry(transientSolid)) { ds.SetShape(new GeometryObject[] { transientSolid }); } else { TessellatedShapeBuilderResult result = GetTessellatedSolid(doc, transientSolid); ds.SetShape(result.GetGeometricalObjects()); } ds.Name = dsName; return(ds); }
private static void SetShape(DirectShape ds, Solid transientSolid) { if (ds.IsValidGeometry(transientSolid)) { ds.SetShape(new GeometryObject[] { transientSolid }); } else { var geoms = transientSolid.TessellateSolid(ds.Document); ds.SetShape(geoms); } }
public static DirectShape CreateRoomDirectShape(Room room) { DirectShape roomShape = null; try { List <GeometryObject> geoList = new List <GeometryObject>(); var solidGeometries = from geoObj in room.ClosedShell where geoObj.GetType() == typeof(Solid) && (geoObj as Solid).Volume != 0 select geoObj; if (solidGeometries.Any()) { geoList = solidGeometries.ToList(); } ElementId categoryId = new ElementId((int)BuiltInCategory.OST_Mass); if (DirectShape.IsValidCategoryId(categoryId, room.Document)) { #if RELEASE2015 || RELEASE2016 roomShape = DirectShape.CreateElement(room.Document, categoryId, "Measure", room.UniqueId); #else roomShape = DirectShape.CreateElement(room.Document, categoryId); #endif } roomShape.SetShape(geoList); } catch (Exception ex) { string message = ex.Message; } return(roomShape); }
/// <summary> /// Create a DirectShape, and set its options accordingly. /// </summary> /// <param name="doc">The document.</param> /// <param name="categoryId">The category of the DirectShape.</param> /// <param name="dataGUID">The GUID of the data creating the DirectShape.</param> /// <param name="geomObjs">The list of geometries to add to the DirectShape.</param> /// <param name="id">The id of the IFCEntity object creating the DirectShape.</param> /// <returns>The DirectShape.</returns> static public DirectShape CreateElement(Document doc, ElementId categoryId, string dataGUID, IList <GeometryObject> geomObjs, int id, IFCEntityType entityType) { string appGUID = Importer.ImportAppGUID(); DirectShape directShape = DirectShape.CreateElement(doc, GetDSValidCategoryId(doc, categoryId, id)); directShape.ApplicationId = appGUID; directShape.ApplicationDataId = dataGUID; // Note: we use the standard options for the DirectShape that is created. This includes but is not limited to: // Referenceable: true. // Room Bounding: if applicable, user settable. if (IFCImportFile.TheFile.Options.UseStreamlinedOptions) { // Disable referencing for performance purposes. DirectShapeOptions options = directShape.GetOptions(); options.ReferencingOption = DirectShapeReferencingOption.NotReferenceable; directShape.SetOptions(options); } // This is used in some places to create something not related to an IFCEntity if (!Importer.TheProcessor.CreateOrUpdateElement(id, dataGUID, entityType.ToString(), categoryId.IntegerValue, geomObjs)) { if (geomObjs != null) { directShape?.SetShape(geomObjs); } } return(directShape); }
/// <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 void CreateSphereDirectShape(Document doc, XYZ center, float radius, string name) { List <Curve> profile = new List <Curve>(); // first create sphere with 2' radius XYZ profile00 = center; XYZ profilePlus = center + new XYZ(0, radius, 0); XYZ profileMinus = center - new XYZ(0, radius, 0); profile.Add(Line.CreateBound(profilePlus, profileMinus)); profile.Add(Arc.Create(profileMinus, profilePlus, center + new XYZ(radius, 0, 0))); CurveLoop curveLoop = CurveLoop.Create(profile); SolidOptions options = new SolidOptions(ElementId.InvalidElementId, ElementId.InvalidElementId); Frame frame = new Frame(center, XYZ.BasisX, -XYZ.BasisZ, XYZ.BasisY); Solid sphere = GeometryCreationUtilities.CreateRevolvedGeometry(frame, new CurveLoop[] { curveLoop }, 0, 2 * Math.PI, options); using (Transaction t = new Transaction(doc, "Create sphere direct shape")) { t.Start(); DirectShape ds = DirectShape.CreateElement(doc, new ElementId(BuiltInCategory.OST_GenericModel)); ds.SetShape(new GeometryObject[] { sphere }); ds.Name = name; t.Commit(); } }
/// <summary> /// Implement this method as an external command for Revit. /// </summary> /// <param name="commandData">An object that is passed to the external application /// which contains data related to the command, /// such as the application object and active view.</param> /// <param name="message">A message that can be set by the external application /// which will be displayed if a failure or cancellation is returned by /// the external command.</param> /// <param name="elements">A set of elements to which the external application /// can add elements that are to be highlighted in case of failure or cancellation.</param> /// <returns>Return the status of the external command. /// A result of Succeeded means that the API external method functioned as expected. /// Cancelled can be used to signify that the user canceled the external operation /// at some point. Failure should be returned if the application is unable to proceed with /// the operation.</returns> public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { _dbdocument = commandData.Application.ActiveUIDocument.Document; try { using (Autodesk.Revit.DB.Transaction tr = new Autodesk.Revit.DB.Transaction(_dbdocument, "CreateNURBS")) { tr.Start(); DirectShape myDirectShape = DirectShape.CreateElement(_dbdocument, new ElementId(BuiltInCategory.OST_GenericModel)); myDirectShape.ApplicationId = "TestCreateNURBS"; myDirectShape.ApplicationDataId = "NURBS"; if (null != myDirectShape) { myDirectShape.SetShape(CreateNurbsSurface()); } tr.Commit(); } } catch (Exception ex) { message = ex.Message; return(Result.Failed); } return(Result.Succeeded); }
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { Document doc = commandData.Application.ActiveUIDocument.Document; Solid box = Box(); Solid sphere = Sphere(); Solid unionSolid = BooleanOperationsUtils.ExecuteBooleanOperation(box, sphere, BooleanOperationsType.Union); Solid intersectSolid = BooleanOperationsUtils.ExecuteBooleanOperation(box, sphere, BooleanOperationsType.Intersect); Solid diffSolid = BooleanOperationsUtils.ExecuteBooleanOperation(box, sphere, BooleanOperationsType.Difference); //Use DirectShape to visualise geometries using (Transaction t = new Transaction(doc, "Transaction")) { t.Start(); GeometryObject[] unionGeomObj = new GeometryObject[] { unionSolid }; GeometryObject[] interGeomObj = new GeometryObject[] { intersectSolid }; GeometryObject[] diffGeomObj = new GeometryObject[] { diffSolid }; DirectShape ds1 = DirectShape.CreateElement(doc, new ElementId(BuiltInCategory.OST_GenericModel)); ds1.SetShape(unionGeomObj); DirectShape ds2 = DirectShape.CreateElement(doc, new ElementId(BuiltInCategory.OST_GenericModel)); ds2.SetShape(interGeomObj); DirectShape ds3 = DirectShape.CreateElement(doc, new ElementId(BuiltInCategory.OST_GenericModel)); ds3.SetShape(diffGeomObj); t.Commit(); } return(Result.Succeeded); }
private bool CreateRoomMass(SpatialElement room) { if (!SpatialElementGeometryCalculator.CanCalculateGeometry(room)) { return(false); } try { SpatialElementGeometryResults results; using (var calculator = new SpatialElementGeometryCalculator(doc)) { results = calculator.CalculateSpatialElementGeometry(room); } using (Solid roomSolid = results.GetGeometry()) { var eid = new ElementId(BuiltInCategory.OST_Mass); DirectShape roomShape = DirectShape.CreateElement(doc, eid); if (roomShape != null && roomSolid.Volume > 0 && roomSolid.Faces.Size > 0) { var geomObj = new GeometryObject[] { roomSolid }; if (geomObj.Length > 0) { roomShape.SetShape(geomObj); CopyAllRoomParametersToMasses(room, roomShape); return(true); } } } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } return(false); }
// Create a DirectShape Sphere static public void CreateSphereDirectShape(Document doc) { List <Curve> profile = new List <Curve>(); // first create sphere with 2' radius XYZ center = XYZ.Zero; double radius = 2.0; //XYZ profile00 = center; XYZ profilePlus = center + new XYZ(0, radius, 0); XYZ profileMinus = center - new XYZ(0, radius, 0); profile.Add(Line.CreateBound(profilePlus, profileMinus)); profile.Add(Arc.Create(profileMinus, profilePlus, center + new XYZ(radius, 0, 0))); CurveLoop curveLoop = CurveLoop.Create(profile); SolidOptions options = new SolidOptions(ElementId.InvalidElementId, ElementId.InvalidElementId); Frame frame = new Frame(center, XYZ.BasisX, -XYZ.BasisZ, XYZ.BasisY); if (Frame.CanDefineRevitGeometry(frame) == true) { Solid sphere = GeometryCreationUtilities.CreateRevolvedGeometry(frame, new CurveLoop[] { curveLoop }, 0, 2 * Math.PI, options); using (Transaction t = new Transaction(doc, "Create sphere direct shape")) { t.Start(); // create direct shape and assign the sphere shape DirectShape ds = DirectShape.CreateElement(doc, new ElementId(BuiltInCategory.OST_GenericModel)); ds.ApplicationId = "Application id"; ds.ApplicationDataId = "Geometry object id"; ds.SetShape(new GeometryObject[] { sphere }); t.Commit(); } } }
/// <summary> /// need open transaction. /// </summary> public static void CreateDirectShape(this Document _doc, IEnumerable <GeometryObject> _geometry, XYZ _translation) { DirectShape ds = DirectShape.CreateElement(_doc, new ElementId(BuiltInCategory.OST_GenericModel)); ds.SetShape(_geometry.ToList()); ElementTransformUtils.MoveElement(ds.Document, ds.Id, _translation); }
/// <summary> /// Create a DirectShape, and set its options accordingly. /// </summary> /// <param name="doc">The document.</param> /// <param name="categoryId">The category of the DirectShape.</param> /// <param name="dataGUID">The GUID of the data creating the DirectShape.</param> /// <param name="geomObjs">The list of geometries to add to the DirectShape.</param> /// <param name="id">The id of the IFCEntity object creating the DirectShape.</param> /// <returns>The DirectShape.</returns> static public DirectShape CreateElement(Document doc, ElementId categoryId, string dataGUID, IList <GeometryObject> geomObjs, int id) { string appGUID = Importer.ImportAppGUID(); DirectShape directShape = DirectShape.CreateElement(doc, GetDSValidCategoryId(doc, categoryId, id)); directShape.ApplicationId = appGUID; directShape.ApplicationDataId = dataGUID; // Note: we use the standard options for the DirectShape that is created. This includes but is not limited to: // Referenceable: true. // Room Bounding: if applicable, user settable. if (IFCImportFile.TheFile.Options.UseStreamlinedOptions) { // Disable referencing for performance purposes. DirectShapeOptions options = directShape.GetOptions(); options.ReferencingOption = DirectShapeReferencingOption.NotReferenceable; directShape.SetOptions(options); } if (directShape != null && geomObjs != null) { directShape.SetShape(geomObjs); } return(directShape); }
public void ProcessMultipleSAT() { List <ElementId> importIds = new List <ElementId>(); // create and set new SAT options SATImportOptions satOptions = new SATImportOptions(); satOptions.Placement = ImportPlacement.Origin; satOptions.ColorMode = ImportColorMode.BlackAndWhite; satOptions.Unit = ImportUnit.Millimeter; using (Transaction trans = new Transaction(m_doc, "UpdateSAT")) { trans.Start(); List <GeometryObject> geoObjList = new List <GeometryObject>(); DirectShape ds = null; ElementId currentId; try { currentId = m_doc.Import(@"B:\Rhino\OpenNURBS\v5_example_file.sat", satOptions, m_doc.ActiveView); importIds.Add(currentId); } catch (Exception) { currentId = ElementId.InvalidElementId; } // extract geometry from import instance ImportInstance ii = m_doc.GetElement(currentId) as ImportInstance; Options gOptions = new Options(); gOptions.ComputeReferences = true; GeometryElement geoElement = ii.get_Geometry(gOptions); // get solids from geometry element List <GeometryObject> tempGeoList = FindElementGeometry(geoElement); foreach (GeometryObject go in tempGeoList) { geoObjList.Add(go); } ds = DirectShape.CreateElement(m_doc, new ElementId((int)BuiltInCategory.OST_GenericModel)); ds.SetShape(geoObjList); // set the Direct Shape options DirectShapeOptions dsOptions = ds.GetOptions(); dsOptions.ReferencingOption = DirectShapeReferencingOption.Referenceable; ds.SetOptions(dsOptions); trans.Commit(); trans.Start("Delete Elements"); // clean up imported solids m_doc.Delete(importIds); trans.Commit(); } }
public DirectShape GetTurningRadiusWithKneeAndToeClearanceDirectShape(XYZ centerPoint) { // The measurements are mentioned in the drawing in inches but we want it to be in internal units (feet) double inch9 = UnitUtils.ConvertToInternalUnits(9, DisplayUnitType.DUT_DECIMAL_INCHES); double inch13 = UnitUtils.ConvertToInternalUnits(13, DisplayUnitType.DUT_DECIMAL_INCHES); double inch21 = UnitUtils.ConvertToInternalUnits(21, DisplayUnitType.DUT_DECIMAL_INCHES); double inch24 = UnitUtils.ConvertToInternalUnits(24, DisplayUnitType.DUT_DECIMAL_INCHES); double inch27 = UnitUtils.ConvertToInternalUnits(27, DisplayUnitType.DUT_DECIMAL_INCHES); double inch30 = UnitUtils.ConvertToInternalUnits(30, DisplayUnitType.DUT_DECIMAL_INCHES); double inch54 = UnitUtils.ConvertToInternalUnits(54, DisplayUnitType.DUT_DECIMAL_INCHES); // First Create a profile to rotate // Let's take center point as the center of the turning circle. XYZ pt1 = new XYZ(0, 0, 0); XYZ pt2 = new XYZ(inch30, 0, 0); XYZ pt3 = new XYZ(inch30, 0, inch9); XYZ pt4 = new XYZ(inch24, 0, inch9); XYZ pt5 = new XYZ(inch21, 0, inch27); XYZ pt6 = new XYZ(inch13, 0, inch27); XYZ pt7 = new XYZ(inch13, 0, inch54); XYZ pt8 = new XYZ(0, 0, inch54); // Document doc = DocumentInterface.getInstance().GetDoc(); Document doc = this.ActiveUIDocument.Document; // Create the profile to rotate List <Curve> profile = new List <Curve>(); profile.Add(Line.CreateBound(pt1, pt2)); profile.Add(Line.CreateBound(pt2, pt3)); profile.Add(Line.CreateBound(pt3, pt4)); profile.Add(Line.CreateBound(pt4, pt5)); profile.Add(Line.CreateBound(pt5, pt6)); profile.Add(Line.CreateBound(pt6, pt7)); profile.Add(Line.CreateBound(pt7, pt8)); profile.Add(Line.CreateBound(pt8, pt1)); DirectShape ds = null; CurveLoop curveLoop = CurveLoop.Create(profile); Solid rotatedSolid = GeometryCreationUtilities.CreateRevolvedGeometry(new Frame(), new CurveLoop[] { curveLoop }, 0, Math.PI * 2.0); using (Transaction transaction = new Transaction(doc, "Create the clearance space solid")) { transaction.Start(); // create direct shape and assign the sphere shape ds = DirectShape.CreateElement(doc, new ElementId(BuiltInCategory.OST_GenericModel)); ds.ApplicationId = "Application id"; ds.ApplicationDataId = "Geometry object id"; ds.SetShape(new GeometryObject[] { rotatedSolid }); ds.Location.Move(centerPoint); // since we have added the solid on origin, the centerPoint - origin (zero) is the direction vector. transaction.Commit(); } return(ds); }
/// <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(); } } }
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); }
internal static ElementId AddMeshToDocument(ref Document doc, Mesh mesh) { ElementId elemId = new ElementId(BuiltInCategory.OST_GenericModel); DirectShape ds = DirectShape.CreateElement(doc, elemId); ds.SetShape(new List <GeometryObject> { mesh }); return(elemId); }
static void GetDirectShape(Document doc, Solid solid) { using (Transaction t = new Transaction(doc, "Create DirectShape object")) { t.Start(); DirectShape ds = DirectShape .CreateElement(doc, new ElementId(BuiltInCategory.OST_GenericModel)); ds.SetShape(new GeometryObject[] { solid }); 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(); } } }
/// <summary> /// Create a DirectShape. /// </summary> /// <param name="doc">The document.</param> /// <param name="categoryId">The category of the DirectShape.</param> /// <param name="dataGUID">The GUID of the data creating the DirectShape.</param> /// <returns>The DirectShape.</returns> static public DirectShape CreateElement(Document doc, ElementId categoryId, string dataGUID, IList <GeometryObject> geomObjs) { string appGUID = Importer.ImportAppGUID(); DirectShape directShape = DirectShape.CreateElement(doc, categoryId, appGUID, dataGUID); if (directShape != null && geomObjs != null) { directShape.SetShape(geomObjs); } return(directShape); }
private bool CreateRoomMassByBoundingBox(Room room) { try { var bb = room.get_BoundingBox(null); var eid = new ElementId(BuiltInCategory.OST_Mass); if (bb == null) { return(false); } #if REVIT2019 || REVIT2018 || REVIT2017 || REVIT2020 || REVIT2021 DirectShape roomShape = DirectShape.CreateElement(doc, eid); #else DirectShape roomShape = DirectShape.CreateElement(doc, eid, "A", "B"); #endif var curves = new List <Curve>(); var bl = new XYZ(bb.Min.X, bb.Min.Y, bb.Min.Z); var br = new XYZ(bb.Max.X, bb.Min.Y, bb.Min.Z); var tr = new XYZ(bb.Max.X, bb.Max.Y, bb.Min.Z); var tl = new XYZ(bb.Min.X, bb.Max.Y, bb.Min.Z); var height = bb.Max.Z - bb.Min.Z; curves.Add(Line.CreateBound(bl, br)); curves.Add(Line.CreateBound(br, tr)); curves.Add(Line.CreateBound(tr, tl)); curves.Add(Line.CreateBound(tl, bl)); var loop = CurveLoop.Create(curves); var options = new SolidOptions(ElementId.InvalidElementId, ElementId.InvalidElementId); var roomSolid = GeometryCreationUtilities.CreateExtrusionGeometry(new[] { loop }, new XYZ(0, 0, 1), height, options); if (roomSolid != null) { var geomObj = new GeometryObject[] { roomSolid }; if (geomObj.Length > 0) { roomShape.SetShape(geomObj); CopyAllRoomParametersToMasses(room, roomShape); return(true); } } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); } return(false); }
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); }
private static void CreateDirectShape(Autodesk.Revit.DB.Document doc, Solid solid, Color color, string paramValue) { OverrideGraphicSettings overrideGraphicSettings = new OverrideGraphicSettings(); DirectShape element = DirectShape.CreateElement(doc, new ElementId((BuiltInCategory) - 2000151)); element.set_ApplicationId("ApplicationID"); element.set_ApplicationDataId("ApplicationDataId"); element.SetShape((IList <GeometryObject>) new GeometryObject[1] { (GeometryObject)solid }); doc.get_ActiveView().SetElementOverrides(((Element)element).get_Id(), overrideGraphicSettings); ((Element)element).get_Parameter((BuiltInParameter) - 1010106).Set(paramValue); }
/// <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(); } } }
/// <summary> /// 使用 DirectShape 显示原点位置 /// </summary> public static void ShowOrigin(this Document _doc) { using (Transaction trans = new Transaction(_doc, "show origin")) { trans.Start(); var lineX = Line.CreateBound(new XYZ(0, 0, 0), new XYZ(1, 0, 0)); var lineY = Line.CreateBound(new XYZ(0, 0, 0), new XYZ(0, 1, 0)); DirectShape ds = DirectShape.CreateElement(_doc, new ElementId(BuiltInCategory.OST_GenericModel)); ds.SetShape(new List <GeometryObject>() { lineX, lineY }); trans.Commit(); } }
public static DirectShape CreateDirectShape(Document doc, IList <GeometryObject> resultList) { DirectShape ds = DirectShape.CreateElement(doc, new ElementId(BuiltInCategory.OST_GenericModel)); ds.ApplicationId = "Application id"; ds.ApplicationDataId = "Geometry object id"; ds.Name = "Load area"; DirectShapeOptions dso = ds.GetOptions(); dso.ReferencingOption = DirectShapeReferencingOption.Referenceable; ds.SetOptions(dso); ds.SetShape(resultList); doc.Regenerate(); return(ds); }
/// <summary> /// Create a DirectShape, and set its options accordingly. /// </summary> /// <param name="doc">The document.</param> /// <param name="categoryId">The category of the DirectShape.</param> /// <param name="dataGUID">The GUID of the data creating the DirectShape.</param> /// <returns>The DirectShape.</returns> static public DirectShape CreateElement(Document doc, ElementId categoryId, string dataGUID, IList <GeometryObject> geomObjs) { string appGUID = Importer.ImportAppGUID(); DirectShape directShape = DirectShape.CreateElement(doc, categoryId, appGUID, dataGUID); // Note: we use the standard options for the DirectShape that is created. This includes but is not limited to: // Referenceable: true. // Room Bounding: if applicable, user settable. if (directShape != null && geomObjs != null) { directShape.SetShape(geomObjs); } return(directShape); }
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); }
private void ImportSTLDocument( STLDocument stlDocument, Document doc, string stlDocumentName) { StlImportProperties properties = StlImportProperties.GetProperties(); using (Transaction t = new Transaction(doc, "Import STL")) { t.Start(); TessellatedShapeBuilder builder = new TessellatedShapeBuilder(); builder.OpenConnectedFaceSet(false); int i = 0; foreach (Facet facet in stlDocument.Facets) { builder.AddFace(FromFacet(facet)); i++; } builder.CloseConnectedFaceSet(); TessellatedShapeBuilderResult result = builder.Build(properties.Target, properties.Fallback, properties.GraphicsStyleId); // Pre-release code from DevDays //DirectShape ds = DirectShape.CreateElement( // doc, result.GetGeometricalObjects(), "A", "B"); //ds.SetCategoryId(new ElementId( // BuiltInCategory.OST_GenericModel)); // Code updated for Revit UR1 ElementId categoryId = new ElementId(BuiltInCategory.OST_GenericModel); DirectShape ds = DirectShape.CreateElement(doc, categoryId, "A", "B"); ds.SetShape(result.GetGeometricalObjects()); ds.Name = stlDocumentName; t.Commit(); } }