Advertisement
chekalin-v

Copy floor. #2

Jul 2nd, 2013
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.59 KB | None | 0 0
  1.     [Transaction(TransactionMode.Manual)]
  2.     public class ElementRoomInfoCommand : IExternalCommand
  3.     {      
  4.         const double _eps = 1.0e-9;
  5.  
  6.         public Result Execute(ExternalCommandData commandData,
  7.             ref string message,
  8.             ElementSet elements)
  9.         {
  10.  
  11.             UIApplication uiapp = commandData.Application;
  12.             UIDocument uidoc = uiapp.ActiveUIDocument;
  13.             Application app = uiapp.Application;
  14.             Document doc = uidoc.Document;  
  15.  
  16.             Reference r;
  17.             try
  18.             {
  19.                 r = uidoc.Selection.PickObject(ObjectType.Element,
  20.                     new FloorSelectionFilter(), "Select a floor");
  21.             }
  22.             catch (Exception)
  23.             {
  24.  
  25.                 return Result.Cancelled;
  26.             }
  27.  
  28.             var floor = doc.get_Element(r.ElementId) as Floor;
  29.  
  30.             using (Transaction t =
  31.                 new Transaction(doc, "Copy floor"))
  32.             {
  33.                 t.Start();
  34.  
  35.                 var newFloor =
  36.                     CopyFloor(floor);
  37.  
  38.                 var moveRes =
  39.                     newFloor.Location.Move(new XYZ(0, 0, 10));
  40.  
  41.                 t.Commit();
  42.  
  43.                 t.Start("Create new floor openings");
  44.  
  45.                 CreateFloorOpenings(floor, newFloor);
  46.  
  47.                 var res = t.Commit();
  48.             }
  49.  
  50.             return Result.Succeeded;
  51.         }
  52.  
  53.         private void CreateFloorOpenings(Floor sourceFloor, Floor destFloor)
  54.         {
  55.             // looking if source floor has openings
  56.  
  57.             var floorGeometryElement =
  58.                sourceFloor.get_Geometry(new Options());
  59.  
  60.             foreach (var geometryObject in floorGeometryElement)
  61.             {
  62.                 var floorSolid =
  63.                     geometryObject as Solid;
  64.  
  65.                 if (floorSolid == null)
  66.                     continue;
  67.  
  68.                 var topFace =
  69.                     GetTopFace(floorSolid);
  70.  
  71.                 if (topFace == null)
  72.                     throw new NotSupportedException("Floor does not have top face");
  73.  
  74.                 if (topFace.EdgeLoops.IsEmpty)
  75.                     throw new NotSupportedException("Floor top face does not have edges");
  76.  
  77.              
  78.                 // if source floor has openings
  79.                 if (topFace.EdgeLoops.Size > 1)
  80.                 {
  81.                     for (int i = 1; i < topFace.EdgeLoops.Size; i++)
  82.                     {
  83.                         var openingEdges =
  84.                             topFace.EdgeLoops.get_Item(i);
  85.  
  86.                         var openingCurveArray =
  87.                             GetCurveArrayFromEdgeArary(openingEdges);
  88.  
  89.                         var opening =
  90.                             sourceFloor
  91.                                 .Document
  92.                                 .Create
  93.                                 .NewOpening(destFloor,
  94.                                             openingCurveArray,
  95.                                             true);
  96.                     }
  97.                 }
  98.                              
  99.             }
  100.         }
  101.  
  102.  
  103.         private Floor CopyFloor(Floor sourceFloor)
  104.         {
  105.             var floorGeometryElement =
  106.                 sourceFloor.get_Geometry(new Options());
  107.  
  108.             foreach (var geometryObject in floorGeometryElement)
  109.             {
  110.                 var floorSolid =
  111.                     geometryObject as Solid;
  112.  
  113.                 if (floorSolid == null)
  114.                     continue;
  115.  
  116.                 var topFace =
  117.                     GetTopFace(floorSolid);
  118.  
  119.                 if (topFace == null)
  120.                     throw new NotSupportedException("Floor does not have top face");
  121.  
  122.                 if (topFace.EdgeLoops.IsEmpty)
  123.                     throw new NotSupportedException("Floor top face does not have edges");
  124.  
  125.                 var outerBoundary =
  126.                     topFace.EdgeLoops.get_Item(0);
  127.  
  128.                 // create new floor using source floor outer boundaries
  129.  
  130.                 CurveArray floorCurveArray =
  131.                     GetCurveArrayFromEdgeArary(outerBoundary);
  132.                
  133.                 var newFloor =
  134.                     sourceFloor
  135.                         .Document
  136.                         .Create
  137.                         .NewFloor(floorCurveArray, false);
  138.  
  139.                 /*
  140.                 // if source floor has openings
  141.                 if (topFace.EdgeLoops.Size > 1)
  142.                 {
  143.                     for (int i = 1; i < topFace.EdgeLoops.Size; i++)
  144.                     {
  145.                         var openingEdges =
  146.                             topFace.EdgeLoops.get_Item(i);
  147.  
  148.                         var openingCurveArray =
  149.                             GetCurveArrayFromEdgeArary(openingEdges);
  150.  
  151.                         var opening =
  152.                             sourceFloor
  153.                                 .Document
  154.                                 .Create
  155.                                 .NewOpening(newFloor,
  156.                                             openingCurveArray,
  157.                                             true);
  158.                     }
  159.                 }
  160.                 */
  161.  
  162.                 return newFloor;
  163.             }
  164.  
  165.             return null;
  166.         }
  167.  
  168.         private CurveArray GetCurveArrayFromEdgeArary(EdgeArray edgeArray)
  169.         {
  170.             CurveArray curveArray =
  171.                 new CurveArray();
  172.  
  173.             foreach (Edge edge in edgeArray)
  174.             {
  175.                 var edgeCurve =
  176.                         edge.AsCurve();
  177.  
  178.                 curveArray.Append(edgeCurve);
  179.             }
  180.  
  181.             return curveArray;
  182.         }
  183.  
  184.  
  185.         PlanarFace GetTopFace(Solid solid)
  186.         {
  187.             PlanarFace topFace = null;
  188.             FaceArray faces = solid.Faces;
  189.             foreach (Face f in faces)
  190.             {
  191.                 PlanarFace pf = f as PlanarFace;
  192.                 if (null != pf
  193.                   && (Math.Abs(pf.Normal.X - 0) < _eps && Math.Abs(pf.Normal.Y - 0) < _eps))
  194.                 {
  195.                     if ((null == topFace)
  196.                       || (topFace.Origin.Z < pf.Origin.Z))
  197.                     {
  198.                         topFace = pf;
  199.                     }
  200.                 }
  201.             }
  202.             return topFace;
  203.         }
  204.     }
  205.  
  206.     public class FloorSelectionFilter : ISelectionFilter
  207.     {
  208.         public bool AllowElement(Element elem)
  209.         {
  210.             return elem is Floor;
  211.         }
  212.  
  213.         public bool AllowReference(Reference reference, XYZ position)
  214.         {
  215.             throw new NotImplementedException();
  216.         }
  217.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement