/// <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 cancelled the external operation /// at some point. Failure should be returned if the application is unable to proceed with /// the operation.</returns> public virtual Result Execute(ExternalCommandData commandData , ref string message, ElementSet elements) { try { Document document = commandData.Application.ActiveUIDocument.Document; Selection choices = commandData.Application.ActiveUIDocument.Selection; IssueMarkerTracking tracking = IssueMarkerTrackingManager.GetInstance().GetTracking(document); // Pick one object from Revit. Reference hasPickOne = choices.PickObject(ObjectType.Element, "Select an element to create a control on."); if (hasPickOne != null && tracking.GetMarkerByElementId(hasPickOne.ElementId) == null) { IssueMarker marker = IssueMarker.Create(document, hasPickOne.ElementId); // Register the marker in tracking. tracking.SubscribeMarker(marker); } return(Result.Succeeded); } catch (Exception ex) { message = ex.Message; return(Result.Failed); } }
/// <summary> /// Perform updates on in-canvas controls. /// In this example, the In-Canvas controls will be deleted, or have their positions changed, depending on the changes to related elements. /// </summary> /// <param name="data">Data about changes in the document.</param> public static void Execute(DocumentChangedEventArgs data) { Document doc = data.GetDocument(); TemporaryGraphicsManager temporaryGraphicsManager = TemporaryGraphicsManager.GetTemporaryGraphicsManager(doc); IssueMarkerTracking tracking = IssueMarkerTrackingManager.GetInstance().GetTracking(doc); foreach (ElementId deleted in data.GetDeletedElementIds()) { if (tracking.GetMarkerByElementId(deleted) is IssueMarker marker) { // This is how to delete control temporaryGraphicsManager.RemoveControl(marker.ControlIndex); // Don't forget to clean up your own data tracking.RemoveMarkerByElement(deleted); } } foreach (ElementId updated in data.GetModifiedElementIds()) { if (tracking.GetMarkerByElementId(updated) is IssueMarker marker) { Element element = doc.GetElement(updated); // Since we keep a copy of InCanvasControlData, we can avoid creating a new one. It already has image and position set - and we can just change the position InCanvasControlData controlData = marker.InCanvasControlData; if (element.Location is LocationPoint pointLoc) { controlData.Position = pointLoc.Point; } else if (element.Location is LocationCurve curveLoc) { controlData.Position = curveLoc.Curve.GetEndPoint(0); } marker.InCanvasControlData = controlData; // This is how to set updated data to a control temporaryGraphicsManager.UpdateControl(marker.ControlIndex, marker.InCanvasControlData); } } }