using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.Attributes;
using System.Threading;
namespace logo
{
[Transaction(TransactionMode.Manual)]
public class Class1 : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIDocument uidoc = commandData.Application.ActiveUIDocument;
Document doc = uidoc.Document;
FloorType fl = new FilteredElementCollector(doc).OfClass(typeof(FloorType)).OfCategory(BuiltInCategory.OST_Floors).FirstOrDefault(x => x.Name == "常规 - 300mm") as FloorType;
Level level = new FilteredElementCollector(doc).OfClass(typeof(Level)).FirstOrDefault(y => y.Name == "标高 1") as Level;
//create curvearray
CurveArray c1 = new CurveArray();
c1.Append(Line.CreateBound(new XYZ(0, 0, 0), new XYZ(0, 33, 0)));
c1.Append(Line.CreateBound(new XYZ(0, 33, 0), new XYZ(33, 33, 0)));
c1.Append(Line.CreateBound(new XYZ(33, 33, 0), new XYZ(33, 0, 0)));
c1.Append(Line.CreateBound(new XYZ(33, 0, 0), new XYZ(0, 0, 0)));
CurveArray c2 = new CurveArray();
c2.Append(Line.CreateBound(new XYZ(33, 33, 0), new XYZ(66, 33, 0)));
c2.Append(Line.CreateBound(new XYZ(66, 33, 0), new XYZ(66, 66, 0)));
c2.Append(Line.CreateBound(new XYZ(66, 66, 0), new XYZ(33, 66, 0)));
c2.Append(Line.CreateBound(new XYZ(33, 66, 0), new XYZ(33, 33, 0)));
CurveArray c3 = new CurveArray();
c3.Append(Line.CreateBound(new XYZ(66, 66, 0), new XYZ(99, 66, 0)));
c3.Append(Line.CreateBound(new XYZ(99, 66, 0), new XYZ(99, 99, 0)));
c3.Append(Line.CreateBound(new XYZ(99, 99, 0), new XYZ(66, 99, 0)));
c3.Append(Line.CreateBound(new XYZ(66, 99, 0), new XYZ(66, 66, 0)));
CurveArray c4 = new CurveArray();
c4.Append(Line.CreateBound(new XYZ(0, 33, 0), new XYZ(33, 33, 0)));
c4.Append(Line.CreateBound(new XYZ(33, 33, 0), new XYZ(33, 66, 0)));
c4.Append(Line.CreateBound(new XYZ(33, 66, 0), new XYZ(0, 66, 0)));
c4.Append(Line.CreateBound(new XYZ(0, 66, 0), new XYZ(0, 33, 0)));
CurveArray c5 = new CurveArray();
c5.Append(Line.CreateBound(new XYZ(0, 66, 0), new XYZ(33, 66, 0)));
c5.Append(Line.CreateBound(new XYZ(33, 66, 0), new XYZ(33, 99, 0)));
c5.Append(Line.CreateBound(new XYZ(33, 99, 0), new XYZ(0, 99, 0)));
c5.Append(Line.CreateBound(new XYZ(0, 99, 0), new XYZ(0, 66, 0)));
CurveArray c6 = new CurveArray();
c6.Append(Line.CreateBound(new XYZ(33, 66, 0), new XYZ(66, 66, 0)));
c6.Append(Line.CreateBound(new XYZ(66, 66, 0), new XYZ(66, 99, 0)));
c6.Append(Line.CreateBound(new XYZ(66, 99, 0), new XYZ(33, 99, 0)));
c6.Append(Line.CreateBound(new XYZ(33, 99, 0), new XYZ(33, 66, 0)));
CurveArray c7 = new CurveArray();
c7.Append(Line.CreateBound(new XYZ(33, 0, 0), new XYZ(66, 0, 0)));
c7.Append(Line.CreateBound(new XYZ(66, 0, 0), new XYZ(66, 33, 0)));
c7.Append(Line.CreateBound(new XYZ(66, 33, 0), new XYZ(33, 33, 0)));
c7.Append(Line.CreateBound(new XYZ(33, 33, 0), new XYZ(33, 0, 0)));
CurveArray c8 = new CurveArray();
c8.Append(Line.CreateBound(new XYZ(66, 0, 0), new XYZ(99, 0, 0)));
c8.Append(Line.CreateBound(new XYZ(99, 0, 0), new XYZ(99, 33, 0)));
c8.Append(Line.CreateBound(new XYZ(99, 33, 0), new XYZ(66, 33, 0)));
c8.Append(Line.CreateBound(new XYZ(66, 33, 0), new XYZ(66, 0, 0)));
CurveArray c9 = new CurveArray();
c9.Append(Line.CreateBound(new XYZ(66, 33, 0), new XYZ(99, 33, 0)));
c9.Append(Line.CreateBound(new XYZ(99, 33, 0), new XYZ(99, 66, 0)));
c9.Append(Line.CreateBound(new XYZ(99, 66, 0), new XYZ(66, 66, 0)));
c9.Append(Line.CreateBound(new XYZ(66, 66, 0), new XYZ(66, 33, 0)));
CurveArrArray caa = new CurveArrArray();
caa.Append(c1);
caa.Append(c7);
caa.Append(c8);
caa.Append(c4);
caa.Append(c2);
caa.Append(c9);
caa.Append(c5);
caa.Append(c6);
caa.Append(c3);
foreach (var item in caa)
{
Transaction trana = new Transaction(doc, "create floor");
trana.Start();
CurveArray cv = item as CurveArray;
Floor f1 = doc.Create.NewFloor(cv, fl, level, false);
trana.Commit();
System.Windows.Forms.Application.DoEvents();
Thread.Sleep(400);
}
//filter floor
IList<Floor> floorlist = new FilteredElementCollector(doc).OfClass(typeof(Floor)).OfCategory(BuiltInCategory.OST_Floors).Cast<Floor>().ToList();
Element floor1 = floorlist.ElementAt(0) as Element;
Element floor2 = floorlist.ElementAt(1) as Element;
Element floor3 = floorlist.ElementAt(2) as Element;
Element floor4 = floorlist.ElementAt(3) as Element;
Element floor5 = floorlist.ElementAt(4) as Element;
Element floor6 = floorlist.ElementAt(5) as Element;
Element floor7 = floorlist.ElementAt(6) as Element;
Element floor8 = floorlist.ElementAt(7) as Element;
Element floor9 = floorlist.ElementAt(8) as Element;
for (int i = 0; i < 255; i += 17)
{
Transaction tran2 = new Transaction(doc, "change floor color");
tran2.Start();
FillPatternElement fp = new FilteredElementCollector(doc).OfClass(typeof(FillPatternElement)).First(x => (x as FillPatternElement).GetFillPattern().IsSolidFill) as FillPatternElement;
OverrideGraphicSettings ogs0 = new OverrideGraphicSettings(doc.ActiveView.GetElementOverrides(floor1.Id));
Color color0 = new Color((byte)i, (byte)0, (byte)0);
ogs0.SetProjectionFillPatternId(fp.Id);
ogs0.SetProjectionFillColor(color0);
doc.ActiveView.SetElementOverrides(floor1.Id, ogs0);
OverrideGraphicSettings ogs1 = new OverrideGraphicSettings(doc.ActiveView.GetElementOverrides(floor2.Id));
Color color1 = new Color((byte)i, (byte)0, (byte)0);
ogs1.SetProjectionFillPatternId(fp.Id);
ogs1.SetProjectionFillColor(color1);
doc.ActiveView.SetElementOverrides(floor2.Id, ogs1);
OverrideGraphicSettings ogs2 = new OverrideGraphicSettings(doc.ActiveView.GetElementOverrides(floor3.Id));
Color color2 = new Color((byte)i, (byte)0, (byte)0);
ogs2.SetProjectionFillPatternId(fp.Id);
ogs2.SetProjectionFillColor(color2);
doc.ActiveView.SetElementOverrides(floor3.Id, ogs2);
OverrideGraphicSettings ogs3 = new OverrideGraphicSettings(doc.ActiveView.GetElementOverrides(floor4.Id));
Color color3 = new Color((byte)i, (byte)0, (byte)0);
ogs2.SetProjectionFillPatternId(fp.Id);
ogs2.SetProjectionFillColor(color3);
doc.ActiveView.SetElementOverrides(floor4.Id, ogs2);
OverrideGraphicSettings ogs4 = new OverrideGraphicSettings(doc.ActiveView.GetElementOverrides(floor5.Id));
Color color4 = new Color((byte)i, (byte)0, (byte)0);
ogs2.SetProjectionFillPatternId(fp.Id);
ogs2.SetProjectionFillColor(color4);
doc.ActiveView.SetElementOverrides(floor5.Id, ogs2);
OverrideGraphicSettings ogs5 = new OverrideGraphicSettings(doc.ActiveView.GetElementOverrides(floor6.Id));
Color color5 = new Color((byte)i, (byte)0, (byte)0);
ogs2.SetProjectionFillPatternId(fp.Id);
ogs2.SetProjectionFillColor(color5);
doc.ActiveView.SetElementOverrides(floor6.Id, ogs2);
OverrideGraphicSettings ogs6 = new OverrideGraphicSettings(doc.ActiveView.GetElementOverrides(floor7.Id));
Color color6 = new Color((byte)i, (byte)0, (byte)0);
ogs2.SetProjectionFillPatternId(fp.Id);
ogs2.SetProjectionFillColor(color6);
doc.ActiveView.SetElementOverrides(floor7.Id, ogs2);
OverrideGraphicSettings ogs7 = new OverrideGraphicSettings(doc.ActiveView.GetElementOverrides(floor8.Id));
Color color7 = new Color((byte)i, (byte)0, (byte)0);
ogs2.SetProjectionFillPatternId(fp.Id);
ogs2.SetProjectionFillColor(color7);
doc.ActiveView.SetElementOverrides(floor8.Id, ogs2);
OverrideGraphicSettings ogs8 = new OverrideGraphicSettings(doc.ActiveView.GetElementOverrides(floor9.Id));
Color color8 = new Color((byte)i, (byte)0, (byte)0);
ogs2.SetProjectionFillPatternId(fp.Id);
ogs2.SetProjectionFillColor(color8);
doc.ActiveView.SetElementOverrides(floor9.Id, ogs2);
tran2.Commit();
System.Windows.Forms.Application.DoEvents();
Thread.Sleep(300);
}
//delete elements
Transaction td1 = new Transaction(doc, "delete elements");
td1.Start();
foreach (var item in floorlist)
{
doc.Delete(item.Id);
}
td1.Commit();
System.Windows.Forms.Application.DoEvents();
//Thread.Sleep(500);
//create new one
CurveArray cnew = new CurveArray();
cnew.Append(Line.CreateBound(new XYZ(0, 0, 0), new XYZ(0, 99, 0)));
cnew.Append(Line.CreateBound(new XYZ(0, 99, 0), new XYZ(99, 99, 0)));
cnew.Append(Line.CreateBound(new XYZ(99, 99, 0), new XYZ(99, 0, 0)));
cnew.Append(Line.CreateBound(new XYZ(99, 0, 0), new XYZ(0, 0, 0)));
Transaction fnew = new Transaction(doc, "create new");
fnew.Start();
Floor f11 = doc.Create.NewFloor(cnew, fl, level, false);
fnew.Commit();
System.Windows.Forms.Application.DoEvents();
Transaction tranx = new Transaction(doc, "change floor color");
tranx.Start();
FillPatternElement fpx = new FilteredElementCollector(doc).OfClass(typeof(FillPatternElement)).First(x => (x as FillPatternElement).GetFillPattern().IsSolidFill) as FillPatternElement;
OverrideGraphicSettings ogss = new OverrideGraphicSettings(doc.ActiveView.GetElementOverrides(f11.Id));
Color colorr = new Color((byte)255, (byte)0, (byte)0);
ogss.SetProjectionFillPatternId(fpx.Id);
ogss.SetProjectionFillColor(colorr);
doc.ActiveView.SetElementOverrides(f11.Id, ogss);
tranx.Commit();
System.Windows.Forms.Application.DoEvents();
Thread.Sleep(500);
//create geo
CurveArray cax = new CurveArray();
cax.Append(Line.CreateBound(new XYZ(54, 16, 0), new XYZ(78, 16, 0)));
Arc arx = Arc.Create(new XYZ(78, 16, 0), new XYZ(78, 22, 0), new XYZ(81, 19, 0));
cax.Append(arx);
cax.Append(Line.CreateBound(new XYZ(78, 22, 0), new XYZ(54, 22, 0)));
cax.Append(Arc.Create(new XYZ(54, 22, 0), new XYZ(54, 16, 0), new XYZ(51, 19, 0)));
Transaction tranz = new Transaction(doc, "create floor");
FloorType ft = new FilteredElementCollector(doc).OfClass(typeof(Floor)).OfCategory(BuiltInCategory.OST_Floors).FirstOrDefault(x => x.Name == "常规 - 150mm") as FloorType;
Level levelx = new FilteredElementCollector(doc).OfClass(typeof(Level)).OfCategory(BuiltInCategory.OST_Levels).FirstOrDefault(y => y.Name == "标高 2") as Level;
tranz.Start();
Floor f = doc.Create.NewFloor(cax, ft, levelx, false);
f.get_Parameter(BuiltInParameter.FLOOR_HEIGHTABOVELEVEL_PARAM).Set(0);
tranz.Commit();
System.Windows.Forms.Application.DoEvents();
Thread.Sleep(200);
//copy element
Transaction copy = new Transaction(doc, "copy element");
copy.Start();
ElementTransformUtils.CopyElement(doc, f.Id, new XYZ(0, 30, 0));
copy.Commit();
System.Windows.Forms.Application.DoEvents();
Thread.Sleep(200);
Transaction copy1 = new Transaction(doc, "copy");
copy1.Start();
ElementTransformUtils.CopyElement(doc, f.Id, new XYZ(0, 60, 0));
copy1.Commit();
System.Windows.Forms.Application.DoEvents();
Thread.Sleep(200);
Transaction copy2 = new Transaction(doc, "copy");
copy2.Start();
ElementTransformUtils.CopyElement(doc, f.Id, new XYZ(40, 0, 0));
copy2.Commit();
System.Windows.Forms.Application.DoEvents();
Thread.Sleep(200);
Transaction copy3 = new Transaction(doc, "copy");
copy3.Start();
ElementTransformUtils.CopyElement(doc, f.Id, new XYZ(40, 30, 0));
copy3.Commit();
System.Windows.Forms.Application.DoEvents();
Thread.Sleep(200);
Transaction copy4 = new Transaction(doc, "copy");
copy4.Start();
ElementTransformUtils.CopyElement(doc, f.Id, new XYZ(40, 60, 0));
copy4.Commit();
System.Windows.Forms.Application.DoEvents();
Thread.Sleep(200);
Transaction copy5 = new Transaction(doc, "copy");
copy5.Start();
ElementTransformUtils.CopyElement(doc, f.Id, new XYZ(80, 0, 0));
copy5.Commit();
System.Windows.Forms.Application.DoEvents();
Thread.Sleep(200);
Transaction copy6 = new Transaction(doc, "copy");
copy6.Start();
ElementTransformUtils.CopyElement(doc, f.Id, new XYZ(80, 30, 0));
copy6.Commit();
System.Windows.Forms.Application.DoEvents();
Thread.Sleep(200);
Transaction copy7 = new Transaction(doc, "copy");
copy7.Start();
ElementTransformUtils.CopyElement(doc, f.Id, new XYZ(80, 60, 0));
copy7.Commit();
System.Windows.Forms.Application.DoEvents();
Thread.Sleep(200);
return Result.Succeeded;
}
}
}