Hi,
I'm trying to get a preview image for a rebar with following method:
var doc = commandData.Application.ActiveUIDocument.Document; var rebars = new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_RebarShape).ToElements().ToArray(); var rebar = rebars.First(); ElementType type = doc.GetElement(rebar.Id) as ElementType; System.Drawing.Size imgSize = new System.Drawing.Size(200, 200); var image = type.GetPreviewImage(imgSize);
But the image is always Null. Is there a other way to do it?
Solved! Go to Solution.
Solved by dragos.turmac. Go to Solution.
the code for getting the rebartype should be:
ElementType type = doc.GetElement(rebar.GetTypeId()) as ElementType;
The hint for getPreviewImage is : This image is similar to what is seen in the Revit UI when selecting the type of an element. So if changing the code does not work, you should check if there is an image in the Revit UI.
Elements of Category OST_RebarShape are OfClass FamilySymbol
Might you be looking for the Class RebarShape ?
Here's code to find the Shape Image
List<Element> _elems = new FilteredElementCollector(doc) .OfClass(typeof(RebarShape)) .ToList(); foreach (Element el in _elems) { RebarShape _rebar = el as RebarShape; if (el == null) continue; Parameter param = _rebar.get_Parameter(BuiltInParameter.REBAR_SHAPE_IMAGE); if (param == null) continue; ElementId ShapeImageId = param.AsElementId(); if (ShapeImageId == ElementId.InvalidElementId) continue; Element _Image = doc.GetElement(ShapeImageId); break; }
what image are you looking for? I have opened the rst_advanced_sample_project and haven't found any rebar with a preview image.
I'm not trying to get an existing image (this doesn't exist for the rebars) but i'm trying to create a image from a rebar like you have in the rebar browser(I know this aren't images) I can get all curves from the rebar at this moment but now i'm stuck how to create an image from this list of DetailCurves
Hi @Anonymous,
You are correct, Rebar types have no preview image, mostly because their shape changes due to various constraints in the host element or parameters; finally, you'll get a preview that has nothing to do with the Rebar from the drawing.
For this reason, you can use GetCurvesForBrowser (and I assume you did). However, because this method does not give you an image but the original curves, it is not exactly trivial to create an image from these lines. One idea that comes across my head would be:
1. Make sure that all Curves returned from GetCurvesForBrowser are in a local coordinate system;
2. Create a bounding box from those lines/points.
3. Depending on the size of bounding box, scale the curves so that the bounding box will have a close width/height with desired picture size;
4. Create a Bitmap that has the exact same size with created bounding box;
5. Iterate through curves and draw lines and other primitives into Bitmap using C#'s graphic tools, e.g.
// Use whatever color you want, be careful about thickness in relation to bitmap size Pen blackPen = new Pen(Color.Black, 3); // Draw line bitmap using(var graphics = Graphics.FromImage(created_bitmap)) { graphics.DrawLine(blackPen, lineX1, lineY1, lineX2, lineY2); }
Once you did above steps, I expect you will have an image containing the shape; depending on shape, it might not be exact because of thickness of the pen, precision loss, bitmap size, etc.
Can't find what you're looking for? Ask the community or share your knowledge.