RevitLookup.Test.TestElements.Mirror C# (CSharp) Method

Mirror() public method

Mirrors a selected modscope
public Mirror ( ) : void
return void
        public void Mirror()
        {
            //Revit.ElementSet elemSet = m_revitApp.ActiveUIDocument.Selection.Elements; // 2015, jeremy: 'Autodesk.Revit.UI.Selection.Selection.Elements' is obsolete: 'This property is deprecated in Revit 2015. Use GetElementIds() and SetElementIds instead.'

              //if( elemSet.IsEmpty )
              //{
              //  MessageBox.Show( "Please select elements to be mirrored" );
              //  return;
              //}

              Autodesk.Revit.DB.Document doc = m_revitApp.ActiveUIDocument.Document;

              ICollection<ElementId> elemSet = m_revitApp.ActiveUIDocument.Selection.GetElementIds(); // 2016, jeremy

              if( 0 == elemSet.Count )
              {
            MessageBox.Show( "Please select elements to be mirrored" );
            return;
              }

              /// a map to hold original elementids and their corresponding
              /// clone elements
              Hashtable origAndCopy = new Hashtable();

              HostObject hostObj;
              Revit.LocationCurve locationCurve = null;

              //Revit.ElementSetIterator elemSetIter = elemSet.ForwardIterator();
              //while( elemSetIter.MoveNext() )

              // first pass will create only host elements, because without them
              // any hosted elements cannot be created

              foreach( ElementId id in elemSet )
              {
            //Revit.Element elem = elemSetIter.Current as Revit.Element;

            Revit.Element elem = doc.GetElement( id );

            /// there are only 4 host objects, of which only wall
            /// and floor can be created for now
            hostObj = elem as HostObject;
            if( hostObj != null )
            {

              Floor floor = elem as Floor;
              if( floor != null )
              {
            // get geometry to figure out location of floor
            Options options = m_revitApp.Application.Create.NewGeometryOptions();
            options.DetailLevel = ViewDetailLevel.Coarse;
            GeometryElement geomElem = floor.get_Geometry( options );
            Solid solid = null;
            foreach( GeometryObject geoObject in geomElem )
            {
              if( geoObject is Solid )
              {
                solid = geoObject as Solid;
                break;
              }
            }
            double absoluteElev = ( (Level) floor.Document.GetElement( floor.LevelId ) ).Elevation + floor.get_Parameter( BuiltInParameter.FLOOR_HEIGHTABOVELEVEL_PARAM ).AsDouble();
            CurveArray curveArray = Utils.Geometry.GetProfile( solid, absoluteElev, m_revitApp.Application );

            // mirror the location
            CurveArray mirroredCurveArray = m_revitApp.Application.Create.NewCurveArray();
            for( int i = 0; i < curveArray.Size; i++ )
            {
              mirroredCurveArray.Append( Utils.Geometry.Mirror( curveArray.get_Item( i ), GeomUtils.kXAxis, m_revitApp.Application ) );
            }

            // create the mirrored floor
            Floor floorClone = m_revitApp.ActiveUIDocument.Document.Create.NewFloor( mirroredCurveArray, floor.FloorType, (Level) floor.Document.GetElement( floor.LevelId ), false );

            // give mirrored floor same param values as original floor
            Utils.ParamUtil.SetParameters( floorClone.Parameters, floor.Parameters );

            // save the original to mirrored floor mapping
            origAndCopy.Add( floor.Id.IntegerValue, floorClone );
            continue;
              }

              Revit.Element elemClone = Utils.Elements.CloneElement( m_revitApp.Application, elem );
              Revit.Location elemLocation = elemClone.Location;

              if( elemLocation != null )
              {
            locationCurve = elemLocation as Revit.LocationCurve;
            if( locationCurve != null )
            {
              locationCurve.Curve = Utils.Geometry.Mirror( locationCurve.Curve, GeomUtils.kXAxis, m_revitApp.Application );
            }
              }

              // save the mapping
              origAndCopy.Add( elem.Id.IntegerValue, elemClone );
            }
              }

              //elemSetIter.Reset();

              Revit.LocationPoint locationPoint = null;

              // second pass will put in the family instances. If it is hosted on an element
              // extract that info from the map and use it
              //while( elemSetIter.MoveNext() )

              foreach( ElementId id in elemSet )
              {
            //Revit.Element elem = elemSetIter.Current as Revit.Element;

            Revit.Element elem = doc.GetElement( id );

            hostObj = elem as HostObject;
            if( hostObj == null )
            {

              FamilyInstance famInst = elem as FamilyInstance;
              if( famInst != null )
              {

            XYZ pt = new XYZ();

            // get location of familyinstance
            locationPoint = famInst.Location as Revit.LocationPoint;
            if( locationPoint != null )
            {
              // mirror the location
              pt = Utils.Geometry.Mirror( locationPoint.Point, GeomUtils.kXAxis );
            }
            locationCurve = famInst.Location as Revit.LocationCurve;
            if( locationCurve != null )
            {
              // mirror the location
              pt = Utils.Geometry.Mirror( locationCurve.Curve.GetEndPoint( 0 ), GeomUtils.kXAxis );
            }

            // if family instance is hosted get it from the map
            Revit.Element elemHost = null;
            if( famInst.Host != null )
            {
              elemHost = origAndCopy[famInst.Host.Id.IntegerValue] as Revit.Element;
            }

            // create mirrored family instance
            FamilyInstance famInstClone = m_revitApp.ActiveUIDocument.Document.Create.NewFamilyInstance( pt, famInst.Symbol, elemHost, (Level) famInst.Document.GetElement( famInst.LevelId ), famInst.StructuralType );

            // give mirrored family instance same param values as original family instance
            Utils.ParamUtil.SetParameters( famInstClone.Parameters, famInst.Parameters );
            continue;
              }

              Grid grid = elem as Grid;
              if( grid != null )
              {
            Curve curve = grid.Curve;

            curve = Utils.Geometry.Mirror( curve, GeomUtils.kXAxis, m_revitApp.Application );

            Line line = curve as Line;
            if( line != null )
            {
                Grid.Create(m_revitApp.ActiveUIDocument.Document, line);
            }

            Arc arc = curve as Arc;
            if( arc != null )
            {
              Grid.Create(m_revitApp.ActiveUIDocument.Document, arc );
            }
            continue;
              }

              Opening opening = elem as Opening;
              if( opening != null )
              {
            // get host from the map
            Revit.Element elemHost = null;
            if( opening.Host != null )
            {
              elemHost = origAndCopy[opening.Host.Id.IntegerValue] as Revit.Element;
            }

            CurveArray curveArray = opening.BoundaryCurves;
            // mirror the location
            CurveArray mirroredCurveArray = m_revitApp.Application.Create.NewCurveArray();
            for( int i = 0; i < curveArray.Size; i++ )
            {
              mirroredCurveArray.Append( Utils.Geometry.Mirror( curveArray.get_Item( i ), GeomUtils.kXAxis, m_revitApp.Application ) );
            }

            m_revitApp.ActiveUIDocument.Document.Create.NewOpening( elemHost, mirroredCurveArray, true );
            continue;
              }

              // TBD: Dimension.Curve always throws an exception
              //Dimension dimension = elem as Dimension;
              //if (dimension != null) {
              //    Curve curve = dimension.Curve;
              //    curve = Utils.Geometry.Mirror(curve, GeomUtils.kXAxis, m_revitApp);

              //    Line line = curve as Line;
              //    if (line != null) {
              //        m_revitApp.ActiveUIDocument.Document.Create.NewDimension(dimension.View, line, dimension.References, dimension.DimensionType);
              //    }
              //    continue;
              //}

              Revit.Element elementClone = Utils.Elements.CloneElement( m_revitApp.Application, elem );
              if( elementClone != null )
              {

            BeamSystem beamSystem = elementClone as BeamSystem;
            if( beamSystem != null )
            {
              CurveArray curveArray = beamSystem.Profile;
              // mirror the location
              CurveArray mirroredCurveArray = m_revitApp.Application.Create.NewCurveArray();
              for( int i = 0; i < curveArray.Size; i++ )
              {
                mirroredCurveArray.Append( Utils.Geometry.Mirror( curveArray.get_Item( i ), GeomUtils.kXAxis, m_revitApp.Application ) );
              }
              beamSystem.Profile = mirroredCurveArray;
            }

            ModelCurve modelCurve = elementClone as ModelCurve;
            if( modelCurve != null )
            {
              Curve curve = modelCurve.GeometryCurve;
              curve = Utils.Geometry.Mirror( curve, GeomUtils.kXAxis, m_revitApp.Application );
              modelCurve.GeometryCurve = curve;
            }

            Revit.Location elemLocation = elementClone.Location;
            if( elemLocation != null )
            {
              locationCurve = elemLocation as Revit.LocationCurve;
              if( locationCurve != null )
              {
                locationCurve.Curve = Utils.Geometry.Mirror( locationCurve.Curve, GeomUtils.kXAxis, m_revitApp.Application );
              }
              locationPoint = elemLocation as Revit.LocationPoint;
              if( locationPoint != null )
              {
                locationPoint.Point = Utils.Geometry.Mirror( locationPoint.Point, GeomUtils.kXAxis );
              }
            }

            continue;
              }
            }
              }
        }