one-click architecture: using the revit api to build your models

40
One-Click Architecture: Using the Revit API to Build Your Models Danny Polkinhorn

Upload: aitana

Post on 11-Jan-2016

66 views

Category:

Documents


1 download

DESCRIPTION

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 - PowerPoint PPT Presentation

TRANSCRIPT

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

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

Danny Polkinhorn

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

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

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

Variables

'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

Commands.vb

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

Starting Point

'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)

Commands.vb

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

Four corners

'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))

Walls.vb

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

GeometryWalls.vb

30’ in Y direction

30’ in X direction

30’ in X direction

30’ in Y direction

NW

Cornerstone(SW)

NE

SE

Page 7: One-Click Architecture: Using the Revit API to Build Your Models
Page 8: One-Click Architecture: Using the Revit API to Build Your Models

Document

Application

(DocCreator)

(AppCreator)

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

CreatorsWalls.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

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

‘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)

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

Transactions

Walls

Roof

Views

SheetCommand

ERROR

SucceededFailed

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

Transactions

'Create a transaction, a bucket of database changes to the Revit modelUsing trans As Transaction = New Transaction( _

uiApp.ActiveUIDocument.Document, "Walls“)

End Using

Walls.vb

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

Transactions

'Create a transaction, a bucket of database changes to the Revit modelUsing trans As Transaction = New Transaction( _

uiApp.ActiveUIDocument.Document, "Walls")

End Using

Walls.vb

'Start the transactiontrans.Start

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

Transactions

'Create a transaction, a bucket of database changes to the Revit modelUsing trans As Transaction = New Transaction( _

uiApp.ActiveUIDocument.Document, "Walls")

End Using

Walls.vb

'Start the transactiontrans.Start

'create the wallsWalls.Insert(Wall.Create(doc, westWall, levelId, False))Walls.Insert(Wall.Create(doc, eastWall, levelId, False))Walls.Insert(Wall.Create(doc, northWall, levelId, False))Walls.Insert(Wall.Create(doc, southWall, levelId, False))

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

Transactions

End Using

Walls.vb

'create the wallsWalls.Insert(Wall.Create(doc, westWall, levelId, False))Walls.Insert(Wall.Create(doc, eastWall, levelId, False))Walls.Insert(Wall.Create(doc, northWall, levelId, False))Walls.Insert(Wall.Create(doc, southWall, levelId, False))

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

Transactions

'Let's make each wall go up to Level 2...

'Use an iterator to loop through all the walls we just createdDim iter As ElementSetIterator = Walls.ForwardIterator

End Using

Walls.vb

'create the wallsWalls.Insert(Wall.Create(doc, westWall, levelId, False))Walls.Insert(Wall.Create(doc, eastWall, levelId, False))Walls.Insert(Wall.Create(doc, northWall, levelId, False))Walls.Insert(Wall.Create(doc, southWall, levelId, False))

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

Transactions

'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

Walls.vb

Do While iter.MoveNextDim 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

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

Transactions

Do While iter.MoveNext...

Loop

'commit the walls to the modeltrans.Commit()

End Using

Walls.vb

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

Add Grids

'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)

Commands.vb

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

Add Families

'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 wallFamilies.AddDesk(point)

Commands.vb

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

Door Location

'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))

Families.vb

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

Door Type

'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 itemscollector.OfCategory(BuiltInCategory.OST_Doors)'Filter the collector for only symbols (types in Revit UI)collector.OfClass(GetType(FamilySymbol))'Grab the first element from the collectorDim doorSymbol As FamilySymbol = collector.FirstElement

Families.vb

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

Add the Door

'Add the doordoor = docCreator.NewFamilyInstance( _

doorLocation, doorSymbol, wall, _wall.Level,

[Structure].StructuralType.NonStructural)

Families.vb

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

Desk Insertion Point

'Insertion pointDim inspt As XYZ = startpoint.Add(New XYZ(8, 8, 0))

Families.vb

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

Desk Insertion Point

'Insertion pointDim inspt As XYZ = startpoint.Add(New XYZ(8, 8, 0))

...

docCreator.NewFamilyInstance( _

inspt, deskSymbol, [Structure].StructuralType.NonStructural)

Families.vb

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

Gut Check time...

• Compile and Run your project• Start the Plugins > External Command >RTC2012 Start

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

Add Views

'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"))

Commands.vb

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

Floor Plan Type

'Get the element ID of the floor plan view type...Dim planID As ElementId = viewFamilyTypes.First.Id

Views.vb

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

Create floor plan

'Get the element ID of the floor plan view type...Dim planID As ElementId = viewFamilyTypes.First.Id

'Create the floor plan viewfloorPlan = ViewPlan.Create(uiApp.ActiveUIDocument.Document, planID, levelId)floorPlan.Name = name

Views.vb

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

Annotation

'Annotate the viewsDimensions.AddDimensions(newGrids, newViews(0))Families.AddDoorTag(newDoor, newViews(0))

Commands.vb

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

Door Tag

'The door's locationDim doorLoc As LocationPoint = door.Location

'Add the door tagdocCreator.NewTag(view, door, False, _

TagMode.TM_ADDBY_CATEGORY, TagOrientation.Horizontal, doorLoc.Point)

Families.vb

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

'Create the sheetsSheets.AddSheets(newViews, "RTC2012 Sheet")

Commands.vb

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

Create Sheet

'Create the sheet and change its nameDim sheet As ViewSheet = docCreator.NewViewSheet(titleblock)sheet.Name = name

Sheets.vb

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

Create Sheet

'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 exceptionIf Viewport.CanAddViewToSheet( _

uiApp.ActiveUIDocument.Document, sheet.Id, view.Id) Then End If

Sheets.vb

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

Create Sheet

'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 exceptionIf 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.Outlinewidth = outline.Max.U - outline.Min.Uheight = outline.Max.V - outline.Min.V

End If

Sheets.vb

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

Create Sheet

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

Sheets.vb

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

Create Sheet

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 sheetDim vp As Viewport = Viewport.Create( _

uiApp.ActiveUIDocument.Document, sheet.Id, view.Id, inspt)

End If

Sheets.vb

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

Create Sheet

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 sheetDim vp As Viewport = Viewport.Create( _

uiApp.ActiveUIDocument.Document, sheet.Id, view.Id, inspt)

'Add the view's width to the insertion pointpt = pt.Add(New XYZ(width + 0.1, 0, 0))

End If

Sheets.vb

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

Gut Check Time #2...

• Compile and Run your project• Start the Plugins > External Command >RTC2012 Start

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

Questions?