Created
September 11, 2012 23:24
-
-
Save pzurek/3702963 to your computer and use it in GitHub Desktop.
A quick F# example for Revit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace FSharpPanelBuilder | |
open System | |
open System.Collections.Generic | |
open Autodesk.Revit | |
open Autodesk.Revit.UI | |
open Autodesk.Revit.Attributes | |
open Autodesk.Revit.DB | |
open Autodesk.Revit.UI.Selection | |
type WallSelectionFilter() = | |
class | |
interface ISelectionFilter with | |
member x.AllowElement(element) = element :? Wall | |
member x.AllowReference(reference, xyz) = false | |
end | |
[<Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)>] | |
type Command() = | |
class | |
interface IExternalCommand with | |
member this.Execute(commandData, message : string byref, elements) = | |
try | |
let uiApp = commandData.Application | |
let app = uiApp.Application | |
let uiDoc = uiApp.ActiveUIDocument | |
let doc = uiDoc.Document | |
let sel = uiDoc.Selection | |
let reference = | |
try | |
sel.PickObject(ObjectType.Element, | |
new WallSelectionFilter(), | |
"Select a wall to split into panels") | |
with | |
| :? Autodesk.Revit.Exceptions.OperationCanceledException -> null | |
if reference = null then | |
TaskDialog.Show("F# Panel Builder", "Operation was canceled") |> ignore | |
Autodesk.Revit.UI.Result.Cancelled | |
else | |
let wall = doc.GetElement(reference.ElementId) :?> Wall | |
let locationCurve = wall.Location :?> LocationCurve | |
let line = locationCurve.Curve :?> Line | |
use transaction = new Transaction(doc) | |
let status = transaction.Start("Building panels") | |
let wallList = new List<ElementId>(1) | |
wallList.Add(reference.ElementId) | |
PartUtils.CreateParts(doc, wallList) | |
doc.Regenerate() | |
let parts = PartUtils.GetAssociatedParts(doc, wall.Id, false, false) | |
let divisions = 15 | |
let origin = line.Origin | |
let delta = line.Direction.Multiply(line.Length / (float)divisions) | |
let shiftDelta = Transform.get_Translation(delta) | |
let rotation = Transform.get_Rotation(origin, XYZ.BasisZ, 0.5 * Math.PI) | |
let wallWidthVector = rotation.OfVector(line.Direction.Multiply(2. * wall.Width)) | |
let mutable intersectionLine = Line.CreateBound(origin + wallWidthVector, | |
origin - wallWidthVector) | |
let curveArray = new List<Curve>() | |
for i = 1 to divisions do | |
intersectionLine <- intersectionLine.get_Transformed(shiftDelta) :?> Line | |
curveArray.Add(intersectionLine) | |
let divisionSketchPlane = SketchPlane.Create(doc, new Plane(XYZ.BasisZ, line.Origin)) | |
let intersectionElementsIds = new List<ElementId>() | |
let partMaker = PartUtils.DivideParts(doc, parts, | |
intersectionElementsIds, | |
curveArray, | |
divisionSketchPlane.Id) | |
doc.ActiveView.PartsVisibility <- PartsVisibility.ShowPartsOnly | |
transaction.Commit() |> ignore | |
Autodesk.Revit.UI.Result.Succeeded | |
with ex -> | |
TaskDialog.Show("F# Panel Builder", ex.Message) |> ignore | |
Autodesk.Revit.UI.Result.Failed | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment