Advertisement
chekalin-v

New family instance on the reference plane

Dec 3rd, 2013
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.47 KB | None | 0 0
  1. [Transaction(TransactionMode.Manual)]
  2.     public class Command : IExternalCommand
  3.     {
  4.         public Result Execute(
  5.           ExternalCommandData commandData,
  6.           ref string message,
  7.           ElementSet elements)
  8.         {
  9.             UIApplication uiapp = commandData.Application;
  10.             UIDocument uidoc = uiapp.ActiveUIDocument;
  11.             Application app = uiapp.Application;
  12.             Document doc = uidoc.Document;
  13.  
  14.             Reference r;
  15.  
  16.             try
  17.             {
  18.                 r = uidoc.Selection.PickObject(ObjectType.Element, new ReferencePlaneSelectionFilter());
  19.             }
  20.             catch (OperationCanceledException)
  21.             {
  22.                 return Result.Cancelled;
  23.             }
  24.  
  25.            
  26.  
  27.                        
  28.  
  29.             var referencePlane = doc.GetElement(r.ElementId) as ReferencePlane;
  30.  
  31.            
  32.             FilteredElementCollector collector =
  33.                 new FilteredElementCollector(doc);
  34.  
  35.             var familySymbol =
  36.                 collector
  37.                     .OfClass(typeof (FamilySymbol))
  38.                     .FirstOrDefault(fs => fs.Name.Equals("Семейство на основе грани")) as FamilySymbol;
  39.  
  40.             using (var t = new Transaction(doc, "Вставка семейства"))
  41.             {
  42.                 t.Start();
  43.  
  44.  
  45.                 for (double x = -1; x <= 1; x += 1)
  46.                 {
  47.                     for (double y = -1; y <= 1; y += 1)
  48.                     {
  49.                         for (double z = -1; z <= 1; z += 1)
  50.                         {
  51.  
  52.  
  53.                             XYZ direction = new XYZ(x, y, z);
  54.                             Debug.Print("X: {0}; Y: {1}; Z: {2}", direction.X, direction.Y, direction.Z);
  55.  
  56.                          
  57.                             FamilyInstance createdFamilyInstance =
  58.                                 CreateFamilyInstanceOnReference(doc, referencePlane.Reference, referencePlane.Plane.Origin, direction, familySymbol);
  59.  
  60.                             if (createdFamilyInstance != null)
  61.                             {
  62.                                 Debug.Print(direction.ToString());
  63.                             }
  64.                         }
  65.                     }
  66.                 }
  67.  
  68.  
  69.                 //var fs1 = doc.Create.NewFamilyInstance(XYZ.Zero, familySymbol, new XYZ(0,0,1), referencePlane,
  70.                 //    StructuralType.NonStructural);
  71.  
  72.                 //var fs = doc.Create.NewFamilyInstance(referencePlane.Reference,
  73.                 //    XYZ.Zero, referencePlane.Normal,
  74.                 //    familySymbol);
  75.                
  76.  
  77.                 t.Commit();
  78.             }
  79.             return Result.Succeeded;
  80.         }
  81.  
  82.         FamilyInstance CreateFamilyInstanceOnReference(Document doc, Reference r, XYZ position, XYZ direction, FamilySymbol fs)
  83.         {
  84.             try
  85.             {
  86.                 return doc.Create.NewFamilyInstance(r, position, direction, fs);
  87.             }
  88.             catch (Exception ex)
  89.             {
  90.                 //Debug.Print(ex.Message);
  91.                 return null;
  92.                
  93.             }
  94.         }
  95.     }
  96.  
  97.     public class ReferencePlaneSelectionFilter : ISelectionFilter
  98.     {
  99.         public bool AllowElement(Element elem)
  100.         {
  101.             return elem is ReferencePlane;
  102.         }
  103.  
  104.         public bool AllowReference(Reference reference, XYZ position)
  105.         {
  106.             throw new NotImplementedException();
  107.         }
  108.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement