One-Click Architecture: Using the Revit API to Build Your

Download Report

Transcript One-Click Architecture: Using the Revit API to Build Your

One-Click Architecture: Using the Revit API to
Build Your Models
Danny Polkinhorn
Setup
•
•
Copy the Addins
– From:
• ...RTC2012.OneClick\Addins
– To:
• %ProgramData%\Autodesk\Revit\Addins\2013 or
• C:\ProgramData\Autodesk\Revit\Addins\2013
Open both Solutions
– ...RTC2012.OneClick\_Start\RTC2012.OneClick_Start.sln
– ...RTC2012.OneClick\_Finish\RTC2012.OneClick_Finish.sln
Variables
Commands.vb
'Create some buckets for the various things we'll be creating
Dim newGrids As ElementSet
Dim newWalls As ElementSet
Dim newViews As ElementSet = New ElementSet
Dim newDoor As FamilyInstance
'Get the application
uiApp = commandData.Application
Starting Point
Commands.vb
'Get a starting point from the user
Dim sel As Selection = uiApp.ActiveUIDocument.Selection
Dim point As XYZ = sel.PickPoint("Please select a cornerstone location")
'Add walls and grids
newWalls = Walls.AddWalls(point)
Four corners
Walls.vb
'this is going to be my return value
Dim walls As ElementSet = New ElementSet
'Create the 4 corners of the building, using the start point
‘as the southwest corner. Make it 30 feet square,
‘Revit API units are in Feet.
Dim swPt As XYZ = startPoint
Dim nwPt As XYZ = startPoint.Add(New XYZ(0, 30, 0))
Dim sePt As XYZ = swPt.Add(New XYZ(30, 0, 0))
Dim nePt As XYZ = swPt.Add(New XYZ(30, 30, 0))
Geometry
NW
30’ in X
direction
30’ in Y
direction
Walls.vb
NE
30’ in Y
direction
30’ in X
direction
Cornerstone
(SW)
SE
Application
(AppCreator)
Document
(DocCreator)
Creators
Walls.vb
'Get our creator objects so we can create lines and grids
Dim appCreator As Autodesk.Revit.Creation.Application = _
uiApp.Application.Create
Dim doc As Autodesk.Revit.DB.Document = _
uiApp.ActiveUIDocument.Document
Dim docCreator As Autodesk.Revit.Creation.Document = doc.Create
‘Create the 4 curve objects that will make up the wall baselines
Dim westWall As Curve = appCreator.NewLineBound(swPt, nwPt)
Dim southWall As Curve = appCreator.NewLineBound(swPt, sePt)
Dim eastWall As Curve = appCreator.NewLineBound(sePt, nePt)
Dim northWall As Curve = appCreator.NewLineBound(nwPt, nePt)
Transactions
Walls
Roof
ERROR
Command
Succeeded
Failed
Views
Sheet
Transactions
Walls.vb
'Create a transaction, a bucket of database changes to the Revit model
Using trans As Transaction = New Transaction( _
uiApp.ActiveUIDocument.Document, "Walls“)
End Using
Transactions
Walls.vb
'Create a transaction, a bucket of database changes to the Revit model
Using trans As Transaction = New Transaction( _
uiApp.ActiveUIDocument.Document, "Walls")
'Start the transaction
trans.Start
End Using
Transactions
Walls.vb
'Create a transaction, a bucket of database changes to the Revit model
Using trans As Transaction = New Transaction( _
uiApp.ActiveUIDocument.Document, "Walls")
'Start the transaction
trans.Start
'create the walls
Walls.Insert(Wall.Create(doc,
Walls.Insert(Wall.Create(doc,
Walls.Insert(Wall.Create(doc,
Walls.Insert(Wall.Create(doc,
End Using
westWall, levelId, False))
eastWall, levelId, False))
northWall, levelId, False))
southWall, levelId, False))
Transactions
Walls.vb
'create the walls
Walls.Insert(Wall.Create(doc,
Walls.Insert(Wall.Create(doc,
Walls.Insert(Wall.Create(doc,
Walls.Insert(Wall.Create(doc,
End Using
westWall, levelId, False))
eastWall, levelId, False))
northWall, levelId, False))
southWall, levelId, False))
Transactions
Walls.vb
'create the walls
Walls.Insert(Wall.Create(doc,
Walls.Insert(Wall.Create(doc,
Walls.Insert(Wall.Create(doc,
Walls.Insert(Wall.Create(doc,
westWall, levelId, False))
eastWall, levelId, False))
northWall, levelId, False))
southWall, levelId, False))
'Let's make each wall go up to Level 2...
'Use an iterator to loop through all the walls we just created
Dim iter As ElementSetIterator = Walls.ForwardIterator
End Using
Transactions
Walls.vb
'Let's make each wall go up to Level 2...
'Use an iterator to loop through all the walls we just created
Dim iter As ElementSetIterator = Walls.ForwardIterator
Do While iter.MoveNext
Dim w As Wall = iter.Current
'This WALL_HEIGHT_TYPE parameter is the name for the Top Constraint
'Use the RevitLookup tool in the SDK to help you discover parameter names.
w.Parameter(BuiltInParameter.WALL_HEIGHT_TYPE).Set( _
levels.Item("Level 2").Id)
Loop
End Using
Transactions
Walls.vb
Do While iter.MoveNext
...
Loop
'commit the walls to the model
trans.Commit()
End Using
Add Grids
Commands.vb
'Get a starting point from the user
Dim sel As Selection = uiApp.ActiveUIDocument.Selection
Dim point As XYZ = sel.PickPoint("Please select a cornerstone location")
'Add walls and grids
newWalls = Walls.AddWalls(point)
newGrids = Grids.AddGridLines(point)
Add Families
Commands.vb
'Get a starting point from the user
Dim sel As Selection = uiApp.ActiveUIDocument.Selection
Dim point As XYZ = sel.PickPoint("Please select a cornerstone location")
'Add walls and grids
newWalls = Walls.AddWalls(point)
newGrids = Grids.AddGridLines(point)
'Add families
newDoor = Families.AddDoor(newWalls(1)) 'east wall
Families.AddDesk(point)
Door Location
Families.vb
'Get the midpoint of the wall, using the location curve
Dim wallCurve As LocationCurve = TryCast(wall.Location, LocationCurve)
Dim doorLocation As XYZ = _
MidPoint(wallCurve.Curve.EndPoint(0), wallCurve.Curve.EndPoint(1))
Door Type
Families.vb
'Create a collector so we can get the door symbol (Door Type in Revit UI)
Dim collector As FilteredElementCollector = _
New FilteredElementCollector(uiApp.ActiveUIDocument.Document)
'Filter the collector for only Door related items
collector.OfCategory(BuiltInCategory.OST_Doors)
'Filter the collector for only symbols (types in Revit UI)
collector.OfClass(GetType(FamilySymbol))
'Grab the first element from the collector
Dim doorSymbol As FamilySymbol = collector.FirstElement
Add the Door
Families.vb
'Add the door
door = docCreator.NewFamilyInstance( _
doorLocation, doorSymbol, wall, _
wall.Level, [Structure].StructuralType.NonStructural)
Desk Insertion Point
Families.vb
'Insertion point
Dim inspt As XYZ = startpoint.Add(New XYZ(8, 8, 0))
Desk Insertion Point
Families.vb
'Insertion point
Dim inspt As XYZ = startpoint.Add(New XYZ(8, 8, 0))
...
docCreator.NewFamilyInstance( _
inspt, deskSymbol, [Structure].StructuralType.NonStructural)
Gut Check time...
•
•
Compile and Run your project
Start the Plugins > External Command >RTC2012 Start
Add Views
Commands.vb
'Make the views
newViews.Insert(Views.AddPlanView("RTC2012 Floor Plan"))
newViews.Insert(Views.AddSectionView(point, "RTC2012 Section"))
newViews.Insert(Views.Add3DView(point, "RTC2012 3D View"))
Floor Plan Type
Views.vb
'Get the element ID of the floor plan view type...
Dim planID As ElementId = viewFamilyTypes.First.Id
Create floor plan
Views.vb
'Get the element ID of the floor plan view type...
Dim planID As ElementId = viewFamilyTypes.First.Id
'Create the floor plan view
floorPlan = ViewPlan.Create(uiApp.ActiveUIDocument.Document, planID, levelId)
floorPlan.Name = name
Annotation
Commands.vb
'Annotate the views
Dimensions.AddDimensions(newGrids, newViews(0))
Families.AddDoorTag(newDoor, newViews(0))
Door Tag
Families.vb
'The door's location
Dim doorLoc As LocationPoint = door.Location
'Add the door tag
docCreator.NewTag(view, door, False, _
TagMode.TM_ADDBY_CATEGORY, TagOrientation.Horizontal, doorLoc.Point)
Commands.vb
'Create the sheets
Sheets.AddSheets(newViews, "RTC2012 Sheet")
Create Sheet
Sheets.vb
'Create the sheet and change its name
Dim sheet As ViewSheet = docCreator.NewViewSheet(titleblock)
sheet.Name = name
Create Sheet
Sheets.vb
'We need to make sure the view hasn't already been added to a sheet
'If we don't check, and the view has been added, we'll get an exception
If Viewport.CanAddViewToSheet( _
uiApp.ActiveUIDocument.Document, sheet.Id, view.Id) Then
End If
Create Sheet
Sheets.vb
'We need to make sure the view hasn't already been added to a sheet
'If we don't check, and the view has been added, we'll get an exception
If Viewport.CanAddViewToSheet( _
uiApp.ActiveUIDocument.Document, sheet.Id, view.Id) Then
'Get the outline of the view so we can determine its width and height
Dim outline As BoundingBoxUV = view.Outline
width = outline.Max.U - outline.Min.U
height = outline.Max.V - outline.Min.V
End If
Create Sheet
Sheets.vb
height = outline.Max.V - outline.Min.V
'Set up the insertion point of the view, the center of the view
Dim inspt As XYZ = pt.Add(New XYZ(width / 2, height / 2, 0))
End If
Create Sheet
Sheets.vb
height = outline.Max.V - outline.Min.V
'Set up the insertion point of the view, the center of the view
Dim inspt As XYZ = pt.Add(New XYZ(width / 2, height / 2, 0))
'Add the viewport which places the view on the sheet
Dim vp As Viewport = Viewport.Create( _
uiApp.ActiveUIDocument.Document, sheet.Id, view.Id, inspt)
End If
Create Sheet
Sheets.vb
height = outline.Max.V - outline.Min.V
'Set up the insertion point of the view, the center of the view
Dim inspt As XYZ = pt.Add(New XYZ(width / 2, height / 2, 0))
'Add the viewport which places the view on the sheet
Dim vp As Viewport = Viewport.Create( _
uiApp.ActiveUIDocument.Document, sheet.Id, view.Id, inspt)
'Add the view's width to the insertion point
pt = pt.Add(New XYZ(width + 0.1, 0, 0))
End If
Gut Check Time #2...
•
•
Compile and Run your project
Start the Plugins > External Command >RTC2012 Start
Questions?