/// <summary> /// Return a view with the given name in the document. /// </summary> private View FindView(Document doc, string name) { // todo: check whether this includes derived types, // which is what we need here. in revit 2009, we used // TypeFilter filter = Create.Filter.NewTypeFilter( // typeof( View ), true ); return(Util.GetFirstElementOfTypeNamed( doc, typeof(View), name) as View); }
public Result Execute( ExternalCommandData commandData, ref string message, ElementSet elements) { UIApplication app = commandData.Application; UIDocument uidoc = app.ActiveUIDocument; Document doc = uidoc.Document; Result rc = Result.Failed; using (Transaction t = new Transaction(doc)) { t.Start("Place a New Sprinkler Instance"); // retrieve the sprinkler family symbol: #if _2010 Filter filter = app.Create.Filter.NewFamilyFilter( _name); List <Element> families = new List <Element>(); doc.get_Elements(filter, families); Family family = null; foreach (Element e in families) { family = e as Family; if (null != family) { break; } } #endif // _2010 Family family = Util.GetFirstElementOfTypeNamed( doc, typeof(Family), _name) as Family; if (null == family) { if (!doc.LoadFamily(_filename, out family)) { message = "Unable to load '" + _filename + "'."; return(rc); } } FamilySymbol sprinklerSymbol = null; //foreach( FamilySymbol fs in family.Symbols ) // 2014 foreach (ElementId id in family.GetFamilySymbolIds()) // 2015 { sprinklerSymbol = doc.GetElement(id) as FamilySymbol; break; } Debug.Assert(null != sprinklerSymbol, "expected at least one sprinkler symbol" + " to be defined in family"); // pick the host ceiling: Element ceiling = Util.SelectSingleElement( uidoc, "ceiling to host sprinkler"); if (null == ceiling || !ceiling.Category.Id.IntegerValue.Equals( (int)BuiltInCategory.OST_Ceilings)) { message = "No ceiling selected."; return(rc); } //Level level = ceiling.Level; //XYZ p = new XYZ( 40.1432351841559, 30.09700395984548, 8.0000 ); // these two methods cannot create the sprinkler on the ceiling: //FamilyInstance fi = doc.Create.NewFamilyInstance( p, sprinklerSymbol, ceiling, level, StructuralType.NonStructural ); //FamilyInstance fi = doc.Create.NewFamilyInstance( p, sprinklerSymbol, ceiling, StructuralType.NonStructural ); // use this overload so get the bottom face of the ceiling instead: // FamilyInstance NewFamilyInstance( Face face, XYZ location, XYZ referenceDirection, FamilySymbol symbol ) // retrieve the bottom face of the ceiling: Options opt = app.Application.Create.NewGeometryOptions(); opt.ComputeReferences = true; GeometryElement geo = ceiling.get_Geometry(opt); PlanarFace ceilingBottom = null; foreach (GeometryObject obj in geo) { Solid solid = obj as Solid; if (null != solid) { foreach (Face face in solid.Faces) { PlanarFace pf = face as PlanarFace; if (null != pf) { XYZ normal = pf.FaceNormal.Normalize(); if (Util.IsVertical(normal) && 0.0 > normal.Z) { ceilingBottom = pf; break; } } } } } if (null != ceilingBottom) { XYZ p = PointOnFace(ceilingBottom); // Create the sprinkler family instance: FamilyInstance fi = doc.Create.NewFamilyInstance( ceilingBottom, p, XYZ.BasisX, sprinklerSymbol); rc = Result.Succeeded; } t.Commit(); } return(rc); }
public Result Execute( ExternalCommandData commandData, ref string message, ElementSet elements) { UIApplication uiapp = commandData.Application; UIDocument uidoc = uiapp.ActiveUIDocument; Application app = uiapp.Application; Document doc = uidoc.Document; // Get a lighting fixture family symbol: FilteredElementCollector symbols = Util.GetElementsOfType(doc, typeof(FamilySymbol), BuiltInCategory.OST_LightingFixtures); FamilySymbol sym = symbols.FirstElement() as FamilySymbol; if (null == sym) { message = "No lighting fixture symbol found."; return(Result.Failed); } // Pick the ceiling: #if _2010 uidoc.Selection.StatusbarTip = "Please select ceiling to host lighting fixture"; uidoc.Selection.PickOne(); Element ceiling = null; foreach (Element e in uidoc.Selection.Elements) { ceiling = e as Element; break; } #endif // _2010 Reference r = uidoc.Selection.PickObject( ObjectType.Element, "Please select ceiling to host lighting fixture"); if (null == r) { message = "Nothing selected."; return(Result.Failed); } // 'Autodesk.Revit.DB.Reference.Element' is // obsolete: Property will be removed. Use // Document.GetElement(Reference) instead. //Element ceiling = r.Element; // 2011 Element ceiling = doc.GetElement(r) as Wall; // 2012 // Get the level 1: Level level = Util.GetFirstElementOfTypeNamed( doc, typeof(Level), "Level 1") as Level; if (null == level) { message = "Level 1 not found."; return(Result.Failed); } // Create the family instance: XYZ p = app.Create.NewXYZ(-43, 28, 0); using (Transaction t = new Transaction(doc)) { t.Start("Place New Lighting Fixture Instance"); FamilyInstance instLight = doc.Create.NewFamilyInstance( p, sym, ceiling, level, StructuralType.NonStructural); t.Commit(); } return(Result.Succeeded); }
public Result Execute( ExternalCommandData commandData, ref string message, ElementSet elements) { Result rc = Result.Failed; UIApplication app = commandData.Application; Document doc = app.ActiveUIDocument.Document; // Check whether the family we are interested in is loaded: #if _2010 List <Element> symbols = new List <Element>(); Filter filterFamily = creApp.Filter.NewFamilyFilter(_family_name); doc.get_Elements(filterFamily, symbols); // the family filter returns both the symbols and the family itself: Family f = null; foreach (Element e in symbols) { if (e is Family) { f = e as Family; } else if (e is FamilySymbol) { FamilySymbol s = e as FamilySymbol; Debug.Print("Family name={0}, symbol name={1}", s.Family.Name, s.Name); } } #endif // _2010 Family f = Util.GetFirstElementOfTypeNamed( doc, typeof(Family), _family_name) as Family; using (Transaction t = new Transaction(doc)) { t.Start("Create Beam Type and Instance"); // If the family was not already loaded, then do so: if (null == f) { if (!doc.LoadFamily(_path, out f)) { message = "Unable to load '" + _path + "'."; } } if (null != f) { Debug.Print("Family name={0}", f.Name); // Pick a symbol for duplication, any one will do, // we select the first: FamilySymbol s = null; //foreach( FamilySymbol s2 in f.Symbols ) // 2014 foreach (ElementId id in f.GetFamilySymbolIds()) // 2015 { s = doc.GetElement(id) as FamilySymbol; break; } Debug.Assert(null != s, "expected at least one symbol to be defined in family"); // Duplicate the existing symbol: ElementType s1 = s.Duplicate("Nuovo simbolo"); s = s1 as FamilySymbol; // Analyse the symbol parameters: foreach (Parameter param in s.Parameters) { Debug.Print("Parameter name={0}, value={1}", param.Definition.Name, param.AsValueString()); } // Define new dimensions for our new type; // the specified parameter name is case sensitive: //s.get_Parameter( "b" ).Set( Util.MmToFoot( 500 ) ); // 2014 //s.get_Parameter( "h" ).Set( Util.MmToFoot( 1000 ) ); // 2014 s.LookupParameter("b").Set(Util.MmToFoot(500)); // 2015 s.LookupParameter("h").Set(Util.MmToFoot(1000)); // 2015 // we can change the symbol name at any time: s.Name = "Nuovo simbolo due"; // insert an instance of our new symbol: Autodesk.Revit.Creation.Application creApp = app.Application.Create; Autodesk.Revit.Creation.Document creDoc = doc.Create; // It is possible to insert a beam, // which normally uses a location line, // by specifying only a location point: //XYZ p = XYZ.Zero; //doc.Create.NewFamilyInstance( p, s, nonStructural ); XYZ p = XYZ.Zero; XYZ q = creApp.NewXYZ(30, 20, 20); // feet Line line = Line.CreateBound(p, q); // Specifying a non-structural type here means no beam // is created, and results in a null family instance: FamilyInstance fi = creDoc.NewFamilyInstance( line, s, null, stBeam); // This creates a visible family instance, // but the resulting beam has no location line // and behaves strangely, e.g. cannot be selected: //FamilyInstance fi = doc.Create.NewFamilyInstance( // p, s, q, null, nonStructural ); //List<Element> levels = new List<Element>(); //doc.get_Elements( typeof( Level ), levels ); //Debug.Assert( 0 < levels.Count, // "expected at least one level in model" ); //Level level = levels[0] as Level; //fi = doc.Create.NewFamilyInstance( // line, s, level, nonStructural ); rc = Result.Succeeded; } t.Commit(); } return(rc); }
public Result Execute( ExternalCommandData commandData, ref string message, ElementSet elements) { Result rc = Result.Failed; UIApplication app = commandData.Application; Document doc = app.ActiveUIDocument.Document; // Check whether the family we are // interested in is loaded: #if _2010 List <Element> symbols = new List <Element>(); Filter filter = app.Create.Filter.NewFamilyFilter( _family_name); doc.get_Elements(filter, symbols); // the family filter returns both the // symbols and the family itself: Family f = null; foreach (Element e in symbols) { if (e is Family) { f = e as Family; } else if (e is FamilySymbol) { FamilySymbol s = e as FamilySymbol; Debug.Print( "Family name={0}, symbol name={1}", s.Family.Name, s.Name); } } #endif Family f = Util.GetFirstElementOfTypeNamed( doc, typeof(Family), _family_name) as Family; // If the family was not already loaded, then do so: if (null == f) { if (!doc.LoadFamily(_path, out f)) { message = "Unable to load '" + _path + "'."; } } if (null != f) { Debug.Print("Family name={0}", f.Name); // Pick a symbol for duplication, any one // will do, we select the first: FamilySymbol s = null; //foreach( FamilySymbol s2 in f.Symbols ) // 2014 foreach (ElementId id in f.GetFamilySymbolIds()) // 2015 { s = doc.GetElement(id) as FamilySymbol; break; } Debug.Assert(null != s, "expected at least one symbol" + " to be defined in family"); // Duplicate the existing symbol: ElementType s1 = s.Duplicate("Nuovo simbolo"); s = s1 as FamilySymbol; // Analyse the symbol parameters: foreach (Parameter param in s.Parameters) { Debug.Print( "Parameter name={0}, value={1}", param.Definition.Name, param.AsValueString()); } // Define new dimensions for our new type; // the specified parameter name is case sensitive: //s.get_Parameter( "Width" ).Set( Util.MmToFoot( 500 ) ); // 2014 //s.get_Parameter( "Depth" ).Set( Util.MmToFoot( 1000 ) ); // 2014 s.LookupParameter("Width").Set(Util.MmToFoot(500)); // 2015 s.LookupParameter("Depth").Set(Util.MmToFoot(1000)); // 2015 // We can change the symbol name at any time: s.Name = "Nuovo simbolo due"; // Insert an instance of our new symbol: XYZ p = XYZ.Zero; doc.Create.NewFamilyInstance( p, s, nonStructural); // For a column, the reference direction is ignored: //XYZ normal = new XYZ( 1, 2, 3 ); //doc.Create.NewFamilyInstance( // p, s, normal, null, nonStructural ); rc = Result.Succeeded; } return(rc); }