Hi,
Based on your code snippet i assume you are learning the RevitTrainingMaterial (Revit Intro Lab5 - ModelCreation)?
Good choice!
As per my previous message mentioned when running the code it is important to check there are two floor plans named "Level 1" and "Level 2" in your document (e.g. use the "basic_sample_project.rvt" to excute your code).
Otherwise, the code will not run correctly.
If you like i have tested (macro) version of this command (CreateWalls) so you can study the code.
Like there are some helper functions introduced (ElementFiltering class, FindElements and FindElement function).
You can just copy and paste the code in your IDE Editor (Sharpdevelop - Macro manager), build it and run as a macro.
It's a good excercise to have your 'same' code running in Visual Studio as in the Macro manager.
Good luck with your further study!
Best regards,
So-Chong
/*
* Created by SharpDevelop.
* User: So-Chong.Li
* Date: 9/17/2015
* Time: 11:58 AM
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.DB.Events;
using Autodesk.Revit.UI.Events;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB.Structure;
namespace RevitIntroLab5
{
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
[Autodesk.Revit.DB.Macros.AddInId("4FD29E64-C1B4-499A-837F-2C28AAAEA031")]
public partial class ThisApplication
{
private void Module_Startup(object sender, EventArgs e)
{
}
private void Module_Shutdown(object sender, EventArgs e)
{
}
#region Revit Macros generated code
private void InternalStartup()
{
this.Startup += new System.EventHandler(Module_Startup);
this.Shutdown += new System.EventHandler(Module_Shutdown);
}
#endregion
public void CreateWalls()
{
UIDocument uidoc = this.ActiveUIDocument;
Document m_rvtDoc = uidoc.Document;
// hard coding the size of the house for simplicity
double width = mmToFeet(10000.0);
double depth = mmToFeet(5000.0);
// get the levels we want to work on.
// Note: hard coding for simplicity. Modify here you use
// a different template.
Level level1 = (Level)ElementFiltering.FindElement(
m_rvtDoc, typeof(Level), "Level 1", null);
if (level1 == null)
{
TaskDialog.Show("Revit Intro Lab", "Cannot find (Level 1). Maybe you use a different template? Try with DefaultMetric.rte.");
}
Level level2 = (Level)ElementFiltering.FindElement(
m_rvtDoc, typeof(Level), "Level 2", null);
if (level2 == null)
{
TaskDialog.Show("Revit Intro Lab", "Cannot find (Level 2). Maybe you use a different template? Try with DefaultMetric.rte.");
}
// set four corner of walls.
// 5th point is for combenience to loop through.
// 1 / 304.8 = 0.0032808398950131
double dx = width / 2.0;
double dy = depth / 2.0;
List<XYZ> pts = new List<XYZ>(5);
pts.Add(new XYZ(-dx, -dy, 0.0));
pts.Add(new XYZ(dx, -dy, 0.0));
pts.Add(new XYZ(dx, dy, 0.0));
pts.Add(new XYZ(-dx, dy, 0.0));
pts.Add(pts[0]);
// flag for structural wall or not.
bool isStructural = true;
// save walls we create.
List<Wall> walls = new List<Wall>(4);
using (Transaction t = new Transaction(m_rvtDoc, "Create Wall"))
{
t.Start();
// loop through list of points and define four walls.
for (int i = 0; i <= 3; i++)
{
// define a base curve from two points.
Line baseCurve = Line.CreateBound(pts[i], pts[i + 1]);
// create a wall using the one of overloaded methods.
Wall aWall = Wall.Create(m_rvtDoc, baseCurve, level1.Id, isStructural);
// set the Top Constraint to Level 2
aWall.get_Parameter(BuiltInParameter.WALL_HEIGHT_TYPE).
Set(level2.Id);
// save the wall.
walls.Add(aWall);
}
// This is important. we need these lines to have shrinkwrap working.
m_rvtDoc.Regenerate();
m_rvtDoc.AutoJoinElements();
t.Commit();
}
}
const double _mmToFeet = 0.0032808399;
public static double mmToFeet(double mmValue)
{
return mmValue * _mmToFeet;
}
public class ElementFiltering
{
/// <summary>
/// Helper function: find a list of element with given Class, Name and Category (optional).
/// </summary>
public static IList<Element> FindElements(
Document rvtDoc,
Type targetType,
string targetName,
Nullable<BuiltInCategory> targetCategory)
{
// First, narrow down to the elements of the given type and category
var collector = new FilteredElementCollector(rvtDoc).OfClass(targetType);
if (targetCategory.HasValue)
{
collector.OfCategory(targetCategory.Value);
}
// Parse the collection for the given names
// Using LINQ query here.
var elems =
from element in collector
where element.Name.Equals(targetName)
select element;
// Put the result as a list of element for accessibility.
return elems.ToList();
}
/// <summary>
/// Helper function: searches elements with given Class, Name and Category (optional),
/// and returns the first in the elements found.
/// This gets handy when trying to find, for example, Level.
/// e.g., FindElement(_doc, GetType(Level), "Level 1")
/// </summary>
public static Element FindElement(
Document rvtDoc,
Type targetType,
string targetName,
Nullable<BuiltInCategory> targetCategory)
{
// Find a list of elements using the overloaded method.
IList<Element> elems = FindElements(rvtDoc, targetType, targetName, targetCategory);
// Return the first one from the result.
if (elems.Count > 0)
{
return elems[0];
}
return null;
}
}
}
}