Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Preview image rebar

7 REPLIES 7
SOLVED
Reply
Message 1 of 8
Anonymous
1640 Views, 7 Replies

Preview image rebar

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?

Tags (3)
7 REPLIES 7
Message 2 of 8
FAIR59
in reply to: Anonymous

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. 

 

Message 3 of 8
Anonymous
in reply to: FAIR59

rebar.GetTypeId() gives as value -1 so it doesn't have a valid type

Message 4 of 8
FAIR59
in reply to: Anonymous

Elements of Category OST_RebarShape are  OfClass FamilySymbol

 

Might you be looking for the Class RebarShape ?

 

 

Rebar_browser.PNG

 

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;
                }
Message 5 of 8
Anonymous
in reply to: FAIR59

All the ShapeImageId's have invalid element id's but i'm going to search more on this

Message 6 of 8
FAIR59
in reply to: Anonymous

what image are you looking for? I have opened the rst_advanced_sample_project and haven't found any rebar with a preview image.

 

Message 7 of 8
Anonymous
in reply to: FAIR59

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

Message 8 of 8
dragos.turmac
in reply to: Anonymous

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.



Turmac Dragos
Pr. SW. Engineer

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Forma Design Contest


Rail Community