Images for your Revit Elements

Here is some code and a video showing how you can associate images with elements in your Revit model and display them when the elements are selected.


public void showElementImage()
{
    UIDocument uidoc = this.ActiveUIDocument;
    Document doc = uidoc.Document;

    // dictionary stores element id values and their corresponding image file URLs
    Dictionary<int,string> dictionary = new Dictionary<int, string>();
    dictionary.Add(181736,"http://3.bp.blogspot.com/_dpc8BBT2JGI/TP92yqwD0HI/AAAAAAAABVI/IKFc4lbNXZY/s1600/IMG_6487.JPG");
    dictionary.Add(181750,"http://2.bp.blogspot.com/-8n-blC0CVeE/T_C-zHQCB3I/AAAAAAAATOg/gmPLrT71qwY/s1600/Yusupov+fancy+floor.jpg");
    dictionary.Add(181784,"http://animatedpizzagifs.com/images/psycadelic-pizza-love.gif");

    // prompt user to select elements
    ElementId id = uidoc.Selection.PickObject(ObjectType.Element).ElementId;

    // give message if no dictionary entry exists for this id value
    if (!dictionary.ContainsKey(id.IntegerValue))
    {
        TaskDialog.Show("Error","No image exists for element id = " + id.IntegerValue);
        return;
    }

    // create a Windows Form
    using (System.Windows.Forms.Form myForm = new System.Windows.Forms.Form())
    {
        // Create a label to indicate the element id
        System.Windows.Forms.Label label = new System.Windows.Forms.Label();
        label.Text = "Image for element id " + id.IntegerValue;
        label.AutoSize = true;
        label.Location = new System.Drawing.Point(5, 5);

        // create a picture box to show the image
        System.Windows.Forms.PictureBox picture = new System.Windows.Forms.PictureBox();
        // the location of the image is the dictionary entry for this element id value
        picture.ImageLocation = dictionary[id.IntegerValue];
        picture.Location = new System.Drawing.Point(5, 25);
        picture.Width = 500;
        picture.Height = 500;
        // stretch or shrink to fit the PictureBox
        picture.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;

        // add controls to the form
        myForm.Controls.Add(label);
        myForm.Controls.Add(picture);

        // set the size & location of the form and display it 
        myForm.Height = picture.Top + picture.Height + 50;
        myForm.Width = picture.Left + picture.Width + 50;
        myForm.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
        myForm.ShowDialog();
    }
}
Advertisements

8 thoughts on “Images for your Revit Elements

  1. How can I do this with shared parameters via a windows.form? So that the user can save or load the pictures via an User Interface of the created windows.form?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s