Revit Intro Lab4 - Element Modification
Revit Intro Lab4 - Element Modification
<VB.NET>VB.NET Version</VB.NET>
Objective: In this lab, we will learn how to modify elements. We’ll learn how to:
Modify an element at element level by changing its properties, parameters and location
Modify an element using transform utility methods, such as Move and Rotate
Tasks: We’ll write a command that prompts the user to pick an element and modify its properties. It
then prompts to pick an element once again and rotate it using utility methods.
1. Pick an element.
2. Modify its family type
3. Modify its parameters
4. Modify its location (Optional)
5. Pick an element
6. Move it by ElementTransformUtils.MoveElement() method
7. Rotate it by ElementTransformUtils.RotateElement() method
Figure 1 shows the sample images of output after running the command that you will be defining in this
lab:
Figure 1. we’ll make modification to an element by changing its properties and
by using transformation utility methods.
1.1 Add a new file and define another external command to your project. Let’s name them as follows:
File name: 4_ElementModification.vb (or .cs)
Command class name: ElementModification
Additional Considerations
We’ll be using the following methods from the ElementFiltering class that we have written in the
previous lab:
ElementFiltering.FindFamilyType()
ElementFiltering.FindElement()
1.2 Like we did in the previous labs, define member variables, e.g., m_rvtApp and m_rvtDoc, to keep DB
level application and document respectively. e.g., :
<VB.NET>
'' Element Modification - learn how to modify elements
<Transaction(TransactionMode.Manual)> _
Public Class ElementModification
Implements IExternalCommand
'' ...
Return Result.Succeeded
End Function
End Class
</VB.NET>
2. Pick an Element
We have already learned how to pick an element in the Lab2. Once again, we can use one of overloaded
PickObject() method to pick an object on the screen:
UIDocument.Selection.PickObject(ObjectType.Element, promptString)
<VB.NET>
'' (1) pick an object on a screen.
Dim ref As Reference = rvtUIDoc.Selection.PickObject( _
ObjectType.Element, "Pick an element")
Dim elem As Element = m_rvtDoc.GetElement(ref)
</VB.NET>
For information that is exposed as directly accessible as Class properties, such as Wall.WallType and
FamilyInstance.Symbol, you can change it directly. Following shows an example of re-assigning a new
family type to a given wall. Here we are using a method FindFamilyType() that we have defined in the
previous lab:
<VB.NET>
'' e.g., an element we are given is a wall.
Dim aWall As Wall = elem
</VB.NET>
</VB.NET>
Below is a sample code with some surrounding support information. For simplicity, we assume we have
a wall. For other kind of objects, you can apply the similar approach.
<VB.NET>
Sub ModifyElementPropertiesWall(ByVal elem As Element)
'' ...
End Sub
</VB.NET>
To change the value of a parameter, you will first retrieve a parameter of your interest, then use “Set”
method to modify the parameter with a new value. There are four overloaded methods. You can change
the value of the following data types:
Set(ElementId)
Set(Double)
Set(Int32)
Set(String value)
The following is a sample usage to change a wall’s “Top Offset” and “Comments” parameters:
<VB.NET>
aWall.Parameter(BuiltInParameter.WALL_TOP_OFFSET).Set(14.0)
aWall.Parameter(BuiltInParameter.ALL_MODEL_INSTANCE_COMMENTS).Set( _
"Modified by API")
</VB.NET>
The code below is an example (with some surrounding support information to show it in the command).
It changes the values of the following parameters for a wall instance:
<VB.NET>
'' (2) change its parameters.
'' constrain top of the wall to the level1 and set an offset.
where mmToFeet() is a simple function that converts unit from millimeter to feet.
<VB.NET>
Const _mmToFeet As Double = 0.0032808399
To change the value of location information, you first retrieve location information from the given
instance, and cast it to Location Curve from an instance. This allows you to access to the curve
information. You then create a new line bound, and assign the new curve to the wall’s location:
<VB.NET>
Dim wallLocation As LocationCurve = aWall.Location
The code below shows an example of moving a wall by (-1000.0, 0., 0.) (with some surrounding support
information to show it in the command):
<VB.NET>
'' (3) change its location, using location curve
Exercise:
Implement a function that takes an instance of element and modify its values such as family
type, parameter values, and location information. (For the purpose of this exercise, you may
assume a given element is a specific type of object, such as a wall or a door.)
Call this function from the main Execute method with the element you have picked.
Note: Existing constrains may affect the result of these modifications. For example, if you have other
walls joined at the ends, and you tried to modify the wall in a way that you violate the constraints, Revit
will not allow you to do so. For the testing purposes, you may want to draw a single self-standing wall,
and run the command.
RevitAPI.chm and Revit Developer Wiki includes sample code that shows the usages of some of these
methods. Please refer to them for variation of various methods.
In our training labs, we will take a look at Move and Rotate as an example. The following is an example
of move or translation of a given element by (10.0, 10.0, 0):
<VB.NET>
'' move by displacement
Dim v As XYZ = New XYZ(10.0, 10.0, 0.0)
ElementTransformUtils.MoveElement(m_rvtDoc, elem.Id, v)
</VB.NET>
And here is an example of rotating a given element by 15 degree (= π/12) around Z-axis:
<VB.NET>
'' rotate by 15 degree around z-axis.
Dim pt1 = XYZ.Zero
Dim pt2 = XYZ.BasisZ
Dim axis As Line = Line.CreateBound(pt1, pt2)
ElementTransformUtils.RotateElement(m_rvtDoc, elem.Id, axis, Math.PI/12.0);
</VB.NET>
Following shows an example of a function that move and rotate a given element:
<VB.NET>
'' modify an element through transform utils methods, Move and Rotate
''
Sub ModifyElementByTransformUtilsMethods(ByVal elem As Element)
End Sub
</VB.NET>
Regeneration of Graphics
One last note: when you have modified an element and that changes result in changes model geometry,
and you need to access to the updated geometry, the graphics need to be regenerated. You can control
this by calling Document.Regenerate() method.
m_rvtDoc.Regenerate()
Exercise:
Implement a function that takes an element, and move and rotate the element by some
displacement and rotation value of your choice.
5. Summary
In this lab, we have learned how to modify elements. We have learned how to:
Modify an element at element level by changing its properties, parameters and location
Modify an element using Document level methods, such as Move and Rotate
In the next lab, we will take a look at how to create elements and build a model in the Revit.