// // (C) Copyright 2003-2019 by Autodesk, Inc. // // Permission to use, copy, modify, and distribute this software in // object code form for any purpose and without fee is hereby granted, // provided that the above copyright notice appears in all copies and // that both that copyright notice and the limited warranty and // restricted rights notice below appear in all supporting // documentation. // // AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS. // AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF // MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE. AUTODESK, INC. // DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE // UNINTERRUPTED OR ERROR FREE. // // Use, duplication, or disclosure by the U.S. Government is subject to // restrictions set forth in FAR 52.227-19 (Commercial Computer // Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii) // (Rights in Technical Data and Computer Software), as applicable. // using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Autodesk.Revit; using Autodesk.Revit.DB; using Autodesk.Revit.UI; using RvtView = Autodesk.Revit.DB.View; namespace Revit.SDK.Samples.CreateFillPattern.CS { /// /// /// public partial class PatternForm : System.Windows.Forms.Form { /// /// Current UI document /// UIDocument docUI; /// /// Current revit document /// Document doc; /// /// Constructor /// public PatternForm(ExternalCommandData commandData) { docUI = commandData.Application.ActiveUIDocument; doc = commandData.Application.ActiveUIDocument.Document; InitializeComponent(); IniTreeView(); } /// /// Select all elements in current document with specified element type filter /// /// Target element type /// List of the elements private List GetAllElements() { ElementClassFilter elementFilter = new ElementClassFilter(typeof(T)); FilteredElementCollector collector = new FilteredElementCollector(doc); collector = collector.WherePasses(elementFilter); return collector.Cast().ToList(); } /// /// Close the window after close button is clicked /// /// This object /// A object contains the event data private void buttonCancel_Click(object sender, EventArgs e) { this.Close(); } /// /// Initial nodes of treeviews /// private void IniTreeView() { this.treeViewLinePattern.Nodes.Clear(); TreeNode iniNode2 = new TreeNode("LinePatterns"); treeViewLinePattern.Nodes.Add(iniNode2); List lstLinePatterns = GetAllElements(); for (int i = 0; i < lstLinePatterns.Count; i++) { TreeNode node = new TreeNode(lstLinePatterns[i].Name); node.Name = lstLinePatterns[i].Id.ToString(); iniNode2.Nodes.Add(node); } this.treeViewFillPattern.Nodes.Clear(); TreeNode iniNode1 = new TreeNode("FillPatterns"); treeViewFillPattern.Nodes.Add(iniNode1); List lstFillPatterns = GetAllElements(); for (int i = 0; i < lstFillPatterns.Count; i++) { TreeNode node = new TreeNode(lstFillPatterns[i].Name); node.Name = i.ToString(); iniNode1.Nodes.Add(node); } } /// /// Create a fillpattern element /// /// The fillpattern name /// Created fillpattern element private FillPatternElement GetOrCreateFacePattern(string patternName) { FillPatternTarget target = FillPatternTarget.Model; FillPatternElement fillPatternElement = FillPatternElement.GetFillPatternElementByName(doc, target, patternName); if (fillPatternElement == null) { //Create a fillpattern with specified angle and spacing FillPattern fillPattern = new FillPattern(patternName, target, FillPatternHostOrientation.ToView, 0.5, 0.5, 0.5); Transaction trans = new Transaction(doc); trans.Start("Create a fillpattern element"); fillPatternElement = FillPatternElement.Create(doc, fillPattern); trans.Commit(); } return fillPatternElement; } /// /// Create a complex fillpattern element /// /// The fillpattern name /// Created fillpattern element private FillPatternElement GetOrCreateComplexFacePattern(string patternName) { FillPatternTarget target = FillPatternTarget.Model; FillPatternElement fillPatternElement = FillPatternElement.GetFillPatternElementByName(doc, target, patternName); if (fillPatternElement == null) { // Create the fill pattern FillPattern fillPattern = new FillPattern(patternName, target, FillPatternHostOrientation.ToHost); // Add grids List grids = new List(); //Horizontal lines. grids.Add(CreateGrid(new UV(0, 0.1), 0.5, 0, 0.55, 1.0, 0.1)); grids.Add(CreateGrid(new UV(0, 0.5), 0.5, 0, 0.55, 1.0, 0.1)); // Vertical lines. grids.Add(CreateGrid(new UV(0, 0.1), 0.55, Math.PI / 2, 0.5, 0.4, 0.6)); grids.Add(CreateGrid(new UV(1.0, 0.1), 0.55, Math.PI / 2, 0.5, 0.4, 0.6)); fillPattern.SetFillGrids(grids); // Create the fill pattern element. Now document is modified; transaction is needed Transaction t = new Transaction(doc, "Create fill pattern"); t.Start(); fillPatternElement = FillPatternElement.Create(doc, fillPattern); t.Commit(); } return fillPatternElement; } /// /// Create a fillgrid /// /// /// /// /// /// /// private FillGrid CreateGrid(UV origin, double offset, double angle, double shift, params double[] segments) { FillGrid fillGrid = new FillGrid(); // The arguments: origin, offset (vertical distance between lines), // angle, shift (delta between location of start point per line) // The last two arguments are the segments: e.g. 1.0 units on, // 0.1 units off (units are Revit units (ft)) fillGrid.Origin = origin; fillGrid.Offset = offset; fillGrid.Angle = angle; fillGrid.Shift = shift; List segmentsList = new List(); foreach (double d in segments) { segmentsList.Add(d); } fillGrid.SetSegments(segmentsList); return fillGrid; } /// /// Create a linepattern element /// /// The linepattern name /// Created linepattern element private LinePatternElement CreateLinePatternElement(string patternName) { //Create list of segments which define the line pattern List lstSegments = new List(); lstSegments.Add(new LinePatternSegment(LinePatternSegmentType.Dot, 0.0)); lstSegments.Add(new LinePatternSegment(LinePatternSegmentType.Space, 0.02)); lstSegments.Add(new LinePatternSegment(LinePatternSegmentType.Dash, 0.03)); lstSegments.Add(new LinePatternSegment(LinePatternSegmentType.Space, 0.02)); LinePattern linePattern = new LinePattern(patternName); linePattern.SetSegments(lstSegments); Transaction trans = new Transaction(doc); trans.Start("Create a linepattern element"); LinePatternElement linePatternElement = LinePatternElement.Create(doc, linePattern); trans.Commit(); return linePatternElement; } /// /// Create a fillpattern element and apply it to target material /// /// This object /// A object contains the event data private void buttonCreateFillPattern_Click(object sender, EventArgs e) { Wall targetWall = GetSelectedWall(); if (targetWall == null) { TaskDialog.Show("Create Fill Pattern", "Before applying a FillPattern to a wall's surfaces, you must first select a wall."); this.Close(); return; } FillPatternElement mySurfacePattern = GetOrCreateFacePattern("MySurfacePattern"); Material targetMaterial = doc.GetElement(targetWall.GetMaterialIds(false).First()) as Material; Transaction trans = new Transaction(doc); trans.Start("Apply fillpattern to surface"); targetMaterial.SurfaceForegroundPatternId = mySurfacePattern.Id; trans.Commit(); this.Close(); } /// /// Create a linepattern element and apply it to grids /// /// This object /// A object contains the event data private void buttonCreateLinePattern_Click(object sender, EventArgs e) { List lstGridTypeIds = new List(); GetSelectedGridTypeIds(lstGridTypeIds); if (lstGridTypeIds.Count == 0) { TaskDialog.Show("Apply To Grids", "Before applying a LinePattern to Grids, you must first select at least one grid."); this.Close(); return; } LinePatternElement myLinePatternElement = CreateLinePatternElement("MyLinePattern"); foreach (ElementId typeId in lstGridTypeIds) { Element gridType = doc.GetElement(typeId); //set the parameter value of End Segment Pattern SetParameter("End Segment Pattern", myLinePatternElement.Id, gridType); } this.Close(); } /// /// Set a parameter value of a target element /// /// The parameter name /// Id value of the parameter /// Target element private void SetParameter(string paramName, ElementId eid, Element elem) { foreach (Parameter param in elem.Parameters) { if (param.Definition.Name == paramName) { Transaction trans = new Transaction(doc); trans.Start("Set parameter value"); param.Set(eid); trans.Commit(); break; } } } /// /// Apply fillpattern to surface /// /// This object /// A object contains the event data private void buttonApplyToSurface_Click(object sender, EventArgs e) { Wall targetWall = GetSelectedWall(); if (targetWall == null) { TaskDialog.Show("Apply To Surface", "Before applying a FillPattern to a wall's surfaces, you must first select a wall."); this.Close(); return; } if (treeViewFillPattern.SelectedNode == null || treeViewFillPattern.SelectedNode.Parent == null) { TaskDialog.Show("Apply To Surface", "Before applying a FillPattern to a wall's surfaces, you must first select one FillPattern."); return; } List lstPatterns = GetAllElements(); int patternIndex = int.Parse(treeViewFillPattern.SelectedNode.Name); Material targetMaterial = doc.GetElement(targetWall.GetMaterialIds(false).First()) as Material; Transaction trans = new Transaction(doc); trans.Start("Apply fillpattern to surface"); targetMaterial.SurfaceForegroundPatternId = lstPatterns[patternIndex].Id; trans.Commit(); this.Close(); } /// /// Get a selected wall /// /// Selected wall private Wall GetSelectedWall() { Wall wall = null; foreach (ElementId elemId in docUI.Selection.GetElementIds()) { Element elem = doc.GetElement(elemId); wall = elem as Wall; if (wall != null) return wall; } return wall; } /// /// Apply fillpatterns to cutting surface /// /// This object /// A object contains the event data private void buttonApplyToCutSurface_Click(object sender, EventArgs e) { Wall targetWall = GetSelectedWall(); if (targetWall == null) { TaskDialog.Show("Apply To CutSurface", "Before applying a FillPattern to a wall's cut surfaces, you must first select a wall."); this.Close(); return; } if (treeViewFillPattern.SelectedNode == null || treeViewFillPattern.SelectedNode.Parent == null) { TaskDialog.Show("Apply To CutSurface", "Before applying a FillPattern to a wall's cutting surfaces, you must first select a FillPattern."); return; } List lstPatterns = GetAllElements(); int patternIndex = int.Parse(treeViewFillPattern.SelectedNode.Name); Material targetMaterial = doc.GetElement(targetWall.GetMaterialIds(false).First()) as Material; Transaction trans = new Transaction(doc); trans.Start("Apply fillpattern to cutting surface"); targetMaterial.CutForegroundPatternId = lstPatterns[patternIndex].Id; trans.Commit(); this.Close(); } /// /// Apply linepattern to grids /// /// This object /// A object contains the event data private void buttonApplyToGrids_Click(object sender, EventArgs e) { List lstGridTypeIds = new List(); GetSelectedGridTypeIds(lstGridTypeIds); if (lstGridTypeIds.Count == 0) { TaskDialog.Show("Apply To Grids", "Before applying a LinePattern to Grids, you must first select at least one grid."); this.Close(); return; } if (treeViewLinePattern.SelectedNode == null || treeViewLinePattern.Parent == null) { TaskDialog.Show("Apply To Grids", "Before applying a LinePattern to Grids, you must first select a LinePattern."); return; } ElementId eid = ElementId.Parse(treeViewLinePattern.SelectedNode.Name); foreach (ElementId typeId in lstGridTypeIds) { Element gridType = doc.GetElement(typeId); //set the parameter value of End Segment Pattern SetParameter("End Segment Pattern", eid, gridType); } this.Close(); } /// /// Get selected GridType Ids /// /// Selected GridType Ids private void GetSelectedGridTypeIds(List lstGridTypeIds) { foreach (ElementId elemId in docUI.Selection.GetElementIds()) { Element elem = doc.GetElement(elemId); Grid grid = elem as Grid; if (grid != null) { ElementId gridTypeId = grid.GetTypeId(); if (!lstGridTypeIds.Contains(gridTypeId)) lstGridTypeIds.Add(gridTypeId); } } } /// /// To create a brick-like patternss /// /// This object /// A object contains the event data private void buttonCreateComplexFillPattern_Click(object sender, EventArgs e) { Wall targetWall = GetSelectedWall(); if (targetWall == null) { TaskDialog.Show("Create Fill Pattern", "Before applying a FillPattern to a wall's surfaces, you must first select a wall."); this.Close(); return; } FillPatternElement mySurfacePattern = GetOrCreateComplexFacePattern("MyComplexPattern"); Material targetMaterial = doc.GetElement(targetWall.GetMaterialIds(false).First()) as Material; Transaction trans = new Transaction(doc); trans.Start("Apply complex fillpattern to surface"); targetMaterial.SurfaceForegroundPatternId = mySurfacePattern.Id; trans.Commit(); this.Close(); } private void PatternForm_Load(object sender, EventArgs e) { } } }