So I am running into this weird bug, where if I run the code below on a regular view, it works just fine, but returns an error in the 3D Assembly view. Can someone shed some light on this?
Cheers!
public class ReferencePointFilter : ISelectionFilter { Document doc = null; public ReferencePointFilter(Document document) { this.doc = document; } public bool AllowElement(Element e) { return true; } public bool AllowReference(Reference r, XYZ p) { if (doc.GetElement(r).GetGeometryObjectFromReference(r) is Point) { return true; } return false; } } [Transaction(TransactionMode.Manual)] public class CmdDimAssistTool : IExternalCommand { public Result Execute( ExternalCommandData commandData, ref string message, ElementSet elements) { // Get application and document objects UIApplication uiApp = commandData.Application; Document doc = uiApp.ActiveUIDocument.Document; UIDocument uidoc = uiApp.ActiveUIDocument; try { IList<Reference> pickedRef = null; Selection sel = uiApp.ActiveUIDocument.Selection; ReferencePointFilter selFilter = new ReferencePointFilter(doc); pickedRef = sel.PickObjects(ObjectType.PointOnElement, selFilter, "Select Two Points"); if (pickedRef.Count() < 2) { throw new Exception("Please select at least two points."); } using (Transaction trans = new Transaction(doc, "MakeDim")) { trans.Start(); Reference ref1 = pickedRef[0]; Reference ref2 = pickedRef[1]; // Get points in model coordinates not family coordinates XYZ coord1 = ref1.GlobalPoint; XYZ coord2 = ref2.GlobalPoint; Line line; Plane geomPlane; if (Equals4DigitPrecision(coord1.X, coord2.X) && Equals4DigitPrecision(coord1.Y, coord2.Y)) { // process as vertical line = Line.CreateBound(new XYZ(coord1.X + 1, coord1.Y, coord1.Z), new XYZ(coord1.X + 1, coord1.Y, coord2.Z)); geomPlane = Plane.CreateByNormalAndOrigin(XYZ.BasisX, line.Evaluate(0.5, true)); } else { // process as horizontal line = Line.CreateBound(new XYZ(coord1.X, coord1.Y + 1, coord1.Z), new XYZ(coord2.X, coord2.Y + 1, coord1.Z)); geomPlane = Plane.CreateByNormalAndOrigin(XYZ.BasisZ, line.Evaluate(0.5, true)); } SketchPlane plane = SketchPlane.Create(doc, geomPlane); // Sketch plane cannot be set on Assembly 3D View, so find a default 3D View and set it there instead var defaultView = new FilteredElementCollector(doc).OfClass(typeof(View3D)).Where(x => x.Name.Contains("{3D}")).FirstOrDefault(); if (defaultView != null) { View v = defaultView as View; v.SketchPlane = plane; } ReferenceArray refs = new ReferenceArray(); foreach (Reference r in pickedRef) { refs.Append(r); } Dimension newDim = doc.Create.NewDimension(doc.ActiveView, line, refs); trans.Commit(); } return Result.Succeeded; } // Catch any exceptions and display them catch (Autodesk.Revit.Exceptions.OperationCanceledException) { return Result.Cancelled; } catch (Exception ex) { message = ex.Message; return Result.Failed; } } private static bool Equals4DigitPrecision(double left, double right) { return Math.Abs(left - right) < 0.0001; } }
Hi Konrad,
What error are you getting, please?
Also, can you create the dimension you are after manually in the same view via the user interface?
If this is anything complicated that needs to be looked at by the development team, they will ask us for a minimal reproducible case:
http://thebuildingcoder.typepad.com/blog/about-the-author.html#1b
Cheers,
Jeremy
Jeremy,
Please see this: https://drive.google.com/file/d/0B3QQaPkepGSkd2I0NTVWX09EZG8/view?usp=sharing
Basically there are a few issues here, but the biggest one is that the 3D View in the Assembly behaves differently than a regular 3D View. You can see that clearly in the UI when switching over to the view, most of the tools on the toolbar are disabled. Now, in the API the critical functionality that is missing is the View.SketchPlane which throws an error "Can't draw model lines in this view" or something similar. Now, using the UI, one can set the Work Plane, so I don't see why the API functionality was disabled.
What I tried doing instead was tricking it, by setting a Work Plane in another view, and hoping that the assembly view would assume that as the current default work plane. So what you see in my code, is me finding another 3d view (I used "{3D}" since that's default 3d view), set the sketch plane there, and then create my dimensions in assembly view. Now, that works to an extent. I was able to set it once, and then it would work for half my dimensions. It doesn't recognize when I switch the workplane on subsequent tries. That's a bummer.
I hope this helps. Oh, and to reproduce this, its important that you try creating a horizontal dimension, and then a vertical one as well in the same assembly view. Switching between vertical and horizontal planes seems to cause issues.
Cheers!
You don't need the workplane or the sketchplane, see the discussion https://forums.autodesk.com/t5/revit-api-forum/how-can-i-create-dimension-line-that-is-not-parallel-...
That's not exactly true. In a 3D view one needs a work plane set for the dimension to be placed on such workplane. Otherwise the dimension is placed somewhere off in space. That's not exactly what I am looking for.
using the UI you set the workplane to control where the dimension is placed. Using the API the dimension is placed on the Line that you provide as an argument for the method
doc.Create.NewDimension(doc.ActiveView, line, refs);
I wish it has worked that way, but unfortunately I am not able to make it work. Not setting a work plane generates this warning:
I might be doing something wrong, so if you care to share a working sample of code it will be my pleasure to test this out.
Cheers!
I'm afraid I've given you false hope. Today I realized that the testing I've done, used a view that had an vertical workplane already in place.
Trying to place horizontal dimensions in that view, gives me the same problems you encountered.
Cheers
+1 for admitting to being wrong, and yes, that's the default state of a Section View. It will have a vertical sketch plane already set on that view. Same for Plans, they already have a Work Plane set to whatever Level they were created from. 3D Views are a little different. 🙂
Jeremy, any ideas?
Dear Konrad,
Nope, no idea.
I tried to look at the file you provided above.
It appears to be a GIF file, dimTools.gif, 9464052 bytes in size, which is of little interest to me.
Furthermore, it cannot be opened by any tool I tried on it.
Where is the reproducible case and sample code?
Cheers,
Jeremy
Jeremy,
It's an animated GIF.
Code that I provided in my original post can be copy pasted into an External Command. To reproduce my steps, you can just watch the animated GIF (it's a little video and any browser and image viewer can play it). Here's a minimal sample Revit file with a macro embedded.
You can't really play it in a 3d Ortho view when its an assembly view. However, you can run that macro in a regular 3d view to see that it works fine on that view. You can build it as an External Command to fully test its shortcomings in the assembly view.
Cheers!
Any news on this front? I would very much appreciate a response even if its just an acknowledgement of an issue at hand.
Thanks!
Dear Konrad,
I am checking with the development team for you...
Cheers,
Jeremy
Has this ever been fixed? Still having this issue. How do we add dimensions to a 3d Assembly view via api?
Hi Jeremy,
Turns out that when you try to re-assign a sketchplane to a View3D in an assembly, you get an error message:
Note that at Line 1871 I'm setting a Sketchplane but not trying to create Model lines:
I also snooped into the Assembly 3D view and found the Sketchplane property of this View is Null, maybe that has something to do with the sketchplane not being settable?
At last, Jeremy, any progress on this issue?
Thank you very much!
Rui
Hello @jeremytammik,
It's few years later & the same issue persists. Any Update on it is highly appreciated. Thanks.
Thank you for bringing this to our attention again. As far as I can tell from the thread above, it has not been submitted as an official development ticket yet. I asked the development team for you whether they can suggest anything off-hand. If not, I will gladly raise a ticket for it. Could you provide a complete minimal reproducible case to include in that, please? Thank you!
https://thebuildingcoder.typepad.com/blog/about-the-author.html#1b
@jeremy_tammik wrote:Thank you for bringing this to our attention again. As far as I can tell from the thread above, it has not been submitted as an official development ticket yet. I asked the development team for you whether they can suggest anything off-hand. If not, I will gladly raise a ticket for it. Could you provide a complete minimal reproducible case to include in that, please? Thank you!
https://thebuildingcoder.typepad.com/blog/about-the-author.html#1b
I believe this was solved in 2021 release. Release notes state:
Added the ability to set a work plane in a 3D Assembly view via the API.