Quantcast
Channel: All Revit API Forum posts
Viewing all articles
Browse latest Browse all 66677

Re: having trouble programmatically placing doors and windows in walls, via an a

$
0
0

I was not, but I think I understand it's use now. the following code is what I have come up with so far, however it fails with a symbol not active.

 

using System;
using System.Collections.Generic;
//using System.IO;
//using System.Linq;

using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.Creation;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;

[TransactionAttribute( TransactionMode.Manual )]
[RegenerationAttribute( RegenerationOption.Manual )]
public class RevitDataPlugin : IExternalCommand {
    private List<FamilySymbol> doorFamilies = new List<FamilySymbol>();

    public Result Execute( ExternalCommandData commandData, ref string message, ElementSet elements ) {
        //Get application and document objects
        UIApplication uiApp = commandData.Application;
        var doc = uiApp.ActiveUIDocument.Document;

        using ( Transaction trans = new Transaction( doc ) ) {
            trans.Start( "WallDataParser" );

            PrepareDoorFamilies( commandData );

            Level level = Level.Create( doc, 0.0 );
            XYZ start = new XYZ( 0.0, 0.0, 0.0 );
            XYZ end = new XYZ( 10.0, 0.0, 0.0 );
            Line wallLine = Line.CreateBound( start, end );
            Wall wall = Wall.Create( doc, wallLine as Line, level.Id, false );

            XYZ doorPoint = new XYZ( 5.0 , 0.0, 0.0 );

            ElementId doorCategoryId = new ElementId( BuiltInCategory.OST_Doors );
            //Opening opening = wall.Document.Create.NewOpening( wall, pntStart, pntEnd );

            FamilySymbol doorSymbol = doorFamilies[0]; // TODO: how?

            doc.Create.NewFamilyInstance( doorPoint, doorSymbol, wall, Autodesk.Revit.DB.Structure.StructuralType.NonStru​ctural );

            doc.Regenerate();

            trans.Commit();
        }

        return Result.Succeeded;
    }

    private void PrepareDoorFamilies( ExternalCommandData commandData ) {
        UIApplication uiApp = commandData.Application;
        var doc = uiApp.ActiveUIDocument.Document;

        // prepare families
        FilteredElementIterator familyIter = new FilteredElementCollector( doc ).OfClass( typeof( Family ) ).GetElementIterator();

        while ( familyIter.MoveNext() ) {

            Family doorFamily = familyIter.Current as Family;

            if ( null == doorFamily.FamilyCategory ) { // some family.FamilyCategory is null
                continue;
            }

            if ( doorFamily.FamilyCategory.Name != doc.Settings.Categories.get_Item( BuiltInCategory.OST_Doors ).Name ) { // FamilyCategory.Name is not 'Doors'
                continue;
            }

            foreach ( ElementId elementId in doorFamily.GetFamilySymbolIds() ) {
                // store the created family
                doorFamilies.Add( ( FamilySymbol )( doc.GetElement( elementId ) ) );
            }

        }
    }
}


Viewing all articles
Browse latest Browse all 66677

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>