/// <summary> /// Removes the ExternallyTaggedBRep created by the CreateBRep command from the DirectShape. /// After that, creates the new ExternallyTaggedBRep with other dimensions and adds it to the DirectShape. /// </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) { Document dbDocument = commandData.Application.ActiveUIDocument.Document; try { // If the UpdateBRep external command is called before the CreateBRep command // or the created DirectShape is manually removed or is not valid now, // we should call CreateBRep command at first. if (null == CreateBRep.CreatedDirectShape || !CreateBRep.CreatedDirectShape.IsValidObject || !CreateBRep.CreatedDirectShape.Document.Equals(dbDocument)) { if (Result.Succeeded != HelperMethods.executeCreateBRepCommand(dbDocument)) { return(Result.Failed); } } using (Autodesk.Revit.DB.Transaction transaction = new Autodesk.Revit.DB.Transaction(dbDocument, "UpdateExternallyTaggedBRep")) { transaction.Start(); // Create BRep with other dimensions than CreateBRep command creates and update the geometry in the DirectShape. ExternallyTaggedBRep resizedTaggedBRep = HelperMethods.createExternallyTaggedPodium(120.0, 20.0, 60.0); if (null == resizedTaggedBRep) { return(Result.Failed); } // Remove the old ExternallyTaggedBRep from the DirectShape. CreateBRep.CreatedDirectShape.RemoveExternallyTaggedGeometry(Podium.ExternalId); // Check that the old ExternallyTaggedBRep is removed from the DirectShape. if (CreateBRep.CreatedDirectShape.HasExternalGeometry(Podium.ExternalId)) { return(Result.Failed); } // Add the new resized ExternallyTaggedBRep to the DirectShape. CreateBRep.CreatedDirectShape.AddExternallyTaggedGeometry(resizedTaggedBRep); transaction.Commit(); } } catch (Exception ex) { message = ex.Message; return(Result.Failed); } return(Result.Succeeded); }
/// <summary> /// Makes the main part of the CreateBRep external command actions. /// See CreateBRep.Execute method summary for the details. /// </summary> /// <param name="document">A Document that will be used for Transaction and DirectShape creation.</param> public static Result executeCreateBRepCommand(Document document) { // Create the ExternallyTaggedBRep named "Podium". ExternallyTaggedBRep taggedBRep = HelperMethods.createExternallyTaggedPodium(40.0, 12.0, 30.0); if (null == taggedBRep) { return(Result.Failed); } using (Autodesk.Revit.DB.Transaction transaction = new Autodesk.Revit.DB.Transaction(document, "CreateExternallyTaggedBRep")) { transaction.Start(); // Create the new DirectShape for this open Document and add the created ExternallyTaggedBRep to this DirectShape. CreateBRep.CreatedDirectShape = HelperMethods.createDirectShapeWithExternallyTaggedBRep(document, taggedBRep); if (null == CreateBRep.CreatedDirectShape) { return(Result.Failed); } // Retrieve the ExternallyTaggedBRep by its ExternalId from the DirectShape and check that the returned BRep is valid. ExternallyTaggedBRep retrievedBRep = CreateBRep.CreatedDirectShape.GetExternallyTaggedGeometry(taggedBRep.ExternalId) as ExternallyTaggedBRep; if (null == retrievedBRep) { return(Result.Failed); } // Retrieve the Face by its ExternalGeometryId from the ExternallyTaggedBRep and check that the returned face is valid. // "faceRiser1" is a hardcoded ExternalGeometryId of the one Face in the "Podium" BRep, see Podium.cs file. Face retrievedFace = retrievedBRep.GetTaggedGeometry(new ExternalGeometryId("faceRiser1")) as Face; if (null == retrievedFace) { return(Result.Failed); } // Retrieve the Edge by its ExternalGeometryId from the ExternallyTaggedBRep and check that the returned edge is valid. // "edgeLeftRiser1" is a hardcoded ExternalGeometryId of the one Edge in the "Podium" BRep, see Podium.cs file. Edge retrievedEdge = retrievedBRep.GetTaggedGeometry(new ExternalGeometryId("edgeLeftRiser1")) as Edge; if (null == retrievedEdge) { return(Result.Failed); } transaction.Commit(); return(Result.Succeeded); } }
/// <summary> /// Creates the new DirectShape and adds the ExternallyTaggedBRep to it. /// </summary> /// <param name="document">A Document that will be used for the DirectShape creation.</param> /// <param name="taggedBRep">An ExternallyTaggedBRep that will be added to the created DirectShape.</param> public static DirectShape createDirectShapeWithExternallyTaggedBRep(Document document, ExternallyTaggedBRep taggedBRep) { DirectShape directShape = DirectShape.CreateElement(document, new ElementId(BuiltInCategory.OST_Stairs)); if (null == directShape) { return(null); } directShape.ApplicationId = "TestCreateExternallyTaggedBRep"; directShape.ApplicationDataId = "ExternallyTaggedBRep"; directShape.AddExternallyTaggedGeometry(taggedBRep); return(directShape); }