Revit中创建IndependentTag遇问题:代码调用失败求助
Fixing Your IndependentTag Creation Code in Revit API
Hey there, let's get your tag creation code working. The main issue right now is that your Execute method isn't implemented—it's just throwing a NotImplementedException, which means the command never actually runs any logic. Let's fix that, plus add the necessary transaction wrapping and proper wall selection.
Here's the corrected code with explanations:
using Autodesk.Revit.Attributes; using Autodesk.Revit.DB; using Autodesk.Revit.UI; using System; [Transaction(TransactionMode.Manual)] public class Tagtest : IExternalCommand { public IndependentTag CreateIndependentTag(Document document, Wall wall) { TaskDialog.Show("Create Independent Tag Method", "Start Of Method Dialog"); // Ensure active view is not a 3D view (tags don't work well in 3D) var view = document.ActiveView; if (view is View3D) { throw new InvalidOperationException("Tags cannot be created in 3D views. Please switch to a 2D view."); } // Define tag mode and orientation var tagMode = TagMode.TM_ADDBY_CATEGORY; var tagOrientation = TagOrientation.Horizontal; // Get wall's midpoint for tag placement var wallLoc = wall.Location as LocationCurve; if (wallLoc == null) { throw new InvalidOperationException("Selected wall does not have a curve location."); } var wallMid = wallLoc.Curve.Evaluate(0.5, true); var wallRef = new Reference(wall); // Create the tag inside the transaction var newTag = IndependentTag.Create(document, view.Id, wallRef, true, tagMode, tagOrientation, wallMid); if (newTag == null) { throw new Exception("Create IndependentTag Failed."); } // Note: Setting the wall type's "Type Mark" will change this value for ALL walls of this type // If you want per-tag custom text, use a tag family that uses an instance parameter instead var wallType = wall.WallType; var typeMarkParam = wallType.LookupParameter("Type Mark"); if (typeMarkParam != null && typeMarkParam.IsReadOnly == false) { typeMarkParam.Set("Hello"); } else { TaskDialog.Show("Warning", "Could not set Type Mark parameter for the wall type."); } // Configure leader settings newTag.LeaderEndCondition = LeaderEndCondition.Free; var elbowPnt = wallMid + new XYZ(5.0, 5.0, 0.0); newTag.LeaderElbow = elbowPnt; var headerPnt = wallMid + new XYZ(10.0, 10.0, 0.0); newTag.TagHeadPosition = headerPnt; TaskDialog.Show("Create Independent Tag Method", "End Of Method Dialog"); return newTag; } public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { UIApplication uiApp = commandData.Application; UIDocument uiDoc = uiApp.ActiveUIDocument; Document doc = uiDoc.Document; try { // Prompt user to select a wall Reference pickedRef = uiDoc.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, "Select a wall to tag"); Element elem = doc.GetElement(pickedRef); Wall wall = elem as Wall; if (wall == null) { message = "Selected element is not a wall."; return Result.Failed; } // All modifications to Revit document must be inside a transaction using (Transaction trans = new Transaction(doc, "Create Independent Tag")) { trans.Start(); CreateIndependentTag(doc, wall); trans.Commit(); } return Result.Succeeded; } catch (Autodesk.Revit.Exceptions.OperationCanceledException) { // User canceled the selection return Result.Cancelled; } catch (Exception ex) { message = ex.Message; return Result.Failed; } } }
Key Fixes & Improvements:
- Implemented the
ExecuteMethod: This is the entry point for your external command. We now get the active document, prompt the user to select a wall, and wrap all model changes in a required transaction. - Added Error Handling: Checks for invalid views, non-wall selections, missing parameters, and handles user cancellation gracefully.
- Clarified Type Mark Behavior: Setting the wall type's "Type Mark" affects all walls of that type. For per-tag unique text, use a tag family bound to an instance parameter instead of a type parameter.
- Proper Transaction Wrapping: All code modifying the Revit document (tag creation, parameter changes) is inside a transaction, which is mandatory for committing changes to the model.
Give this code a try—when you run the command, you'll be prompted to select a wall, and it should create the tag with the specified leader positions and "Hello" as the Type Mark.
内容的提问来源于stack exchange,提问作者Chris Hildebran
相关产品推荐
相关产品推荐

