creating walls and getting the levels

creating walls and getting the levels

Anonymous
Not applicable
2,628 Views
5 Replies
Message 1 of 6

creating walls and getting the levels

Anonymous
Not applicable

public List<Wall> CreateWalls()
{

double width = ElementModification.ElementModification.mmToFeet(10000.0);
double depth = ElementModification.ElementModification.mmToFeet(5000.0);

 

Level level1 = (Level)ElementFilter.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 diffrent template? Try with DefaultMetric.rte.");
return null;
}

 

im trying to learn how to creat wall but there is some problem. plz help

when i debug this code, it on Revit shows up Null command  "Cannot find (Level 1). Maybe you use a diffrent template? Try with DefaultMetric.rte. and i also using DefaultMetric.rte.

how to solve this problem?

0 Likes
Accepted solutions (1)
2,629 Views
5 Replies
Replies (5)
Message 2 of 6

Anonymous
Not applicable

I'm not sure but I would use FilteredElementCollector to Levels and then - depending on which wall type you need - use the the Wall.Create Method:

(Document, IList(Curve), ElementId, ElementId, Boolean).

Edit: If you want to adjust height and offset you should use another method:

(Document, Curve, ElementId, ElementId, Double, Double, Boolean, Boolean)

 

See www.revitapidocs.com for more information.

 

0 Likes
Message 3 of 6

so-chong
Advocate
Advocate

Hi,

 

Did you check your Revit document (rvt) contains view names "Level 1" and "Level 2"?

You can check that in your project browser

Level 1and2.png

0 Likes
Message 4 of 6

naveen.kumar.t
Autodesk Support
Autodesk Support

Hi @Anonymous,

try using the below code

To get the level

 FilteredElementCollector COllector = new FilteredElementCollector(doc).OfClass(typeof(Level));
                var ele = from Element in COllector where Element.Name == "Level 1" select Element;
                Level lev = ele.Cast<Level>().ElementAt<Level>(0);

To create the wall

using (Transaction walltrans = new Transaction(doc))
                {
                    walltrans.Start("transaction name");
                    XYZ start;
                    XYZ end;
                    Line geoline1 = Line.CreateBound(start1, end1);
                    Wall w1 = Wall.Create(doc, geoline1, lev.Id, true);
                    walltrans.Commit();
                }

Naveen Kumar T
Developer Technical Services
Autodesk Developer Network

Message 5 of 6

naveen.kumar.t
Autodesk Support
Autodesk Support

Hi @Anonymous,

If it solves your issue could you please accept it as solution!


Naveen Kumar T
Developer Technical Services
Autodesk Developer Network

0 Likes
Message 6 of 6

so-chong
Advocate
Advocate
Accepted solution

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;
		    }
			
		}
	}
}
0 Likes