Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
NinhPham704 committed Oct 3, 2023
2 parents d1e7dcf + aea56da commit 591760b
Show file tree
Hide file tree
Showing 8 changed files with 353 additions and 56 deletions.
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[*.cs]

# CS0162: Unreachable code detected
dotnet_diagnostic.CS0162.severity = silent
5 changes: 5 additions & 0 deletions BimIshou.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ VisualStudioVersion = 17.6.33723.286
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BimIshou", "BimIshou.csproj", "{D86AA2CF-33F9-4175-A0AF-DC69EB72D90B}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{7089C9C6-1F01-4AE0-8AE0-8077AE421750}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestApp", "..\PhamNinh\TestApp\TestApp.csproj", "{132DFDF3-D96D-4EF9-9FA8-92F7B5C52F8F}"
EndProject
Global
Expand Down
57 changes: 57 additions & 0 deletions Commands/Class1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using Autodesk.Revit.DB;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BimIshou.Commands
{
internal class Class1
{
Document doc;
private void _editPlanRegion(Element planRegion, List<Line> polygon)
{
using Transaction transTemp = new Transaction(doc, "temp");
transTemp.Start();
ICollection<ElementId> ids = doc.Delete(planRegion.Id);
transTemp.RollBack();

//Get detailines of plan region
List<DetailLine> detailLines = new List<DetailLine>();
foreach (ElementId id in ids)
{
Element ele = doc.GetElement(id);
if (ele is DetailLine)
{
detailLines.Add(ele as DetailLine);
}
}

using Transaction trans = new Transaction(doc);
trans.Start("Change Plane Region");

//Get 1 detailine
var firstDetailLine = detailLines.FirstOrDefault();

//Get z
var z = firstDetailLine.GeometryCurve.GetEndPoint(0).Z;

//Create new detail line
foreach (var line in polygon)
{
Transform transform = Transform.CreateTranslation(new XYZ(0, 0, 1));
var tf = Transform.Identity;
var newID = ElementTransformUtils.CopyElement(doc, firstDetailLine.Id, XYZ.BasisY);
var newLine = doc.GetElement(newID.First()) as DetailLine;
var locCurve = newLine.Location as LocationCurve;
locCurve.Curve = Line.CreateBound(transform.OfPoint(line.GetEndPoint(0)), transform.OfPoint(line.GetEndPoint(1)));
}

//Delete old detail lines
doc.Delete(detailLines.Select(x => x.Id).ToList());

trans.Commit();
}
}
}
19 changes: 4 additions & 15 deletions Commands/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,36 +54,28 @@ private void Application_DocumentChanged(object sender, Autodesk.Revit.DB.Events

[UsedImplicitly]
[Transaction(TransactionMode.Manual)]
public class PostCommanCreateCeiling : ExternalCommand
public class PostCommanCreateCeiling
{
//public static bool ReadyToAdd;
public static List<ElementId> AddedElement;
public static UIDocument UIDoc;
public static UIDocument uidoc;
public static UIApplication uiapp;
public static Autodesk.Revit.ApplicationServices.Application app;
public static event EventHandler OnPostableCommandModelLineEnded;
public override void Execute()
public void Execute()
{
//ReadyToAdd = true;
AddedElement = new List<ElementId>();
OnPostableCommandModelLineEnded = null;
RevitCommandEndedMonitor revitCommandEndedMonitor = new RevitCommandEndedMonitor(uiapp);
revitCommandEndedMonitor.CommandEnded += RevitCommandEndedMonitor_CommandEnded;
app.DocumentChanged += Application_DocumentChanged;

RevitCommandId cmdModelLine_id = RevitCommandId.LookupPostableCommandId(PostableCommand.AutomaticCeiling);
uiapp.PostCommand(cmdModelLine_id);
}
/// <summary>
/// Register OnPostableCommandModelLineEnded after start
/// Start on any Cmd
/// Finist => List<DetailLine> AddedDetailLines
/// </summary>
/// <param name="uidoc">UIDocument</param>
public static void Start(UIApplication uiap)
{
uiapp = uiap;
UIDoc = uiapp.ActiveUIDocument;
uidoc = uiapp.ActiveUIDocument;
app = uiapp.Application;
new PostCommanCreateCeiling().Execute();
}
Expand Down Expand Up @@ -125,12 +117,9 @@ private void OnRevitUiApplicationIdling(object sender, IdlingEventArgs idlingEve
_initializingCommandMonitor = false;
return;
}

_revitUiApplication.Idling -= OnRevitUiApplicationIdling;

OnCommandEnded();
}

protected virtual void OnCommandEnded()
{
CommandEnded?.Invoke(this, EventArgs.Empty);
Expand Down
117 changes: 117 additions & 0 deletions Commands/CreateCeiling.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.UI;
using BimIshou.Utils;
using Nice3point.Revit.Toolkit.External;

namespace BimIshou.Commands
{
[Transaction(TransactionMode.Manual)]
public class CreateCeilling : ExternalCommand
{
RevitCommandEndedMonitor revitCommandEndedMonitor;
IList<Reference> selectRooms;
public List<ElementId> AddedElement { get; set; } = new List<ElementId>();
List<ElementId> Ceillings = new();
public override void Execute()
{
try
{
selectRooms = UiDocument.Selection.PickObjects(Autodesk.Revit.UI.Selection.ObjectType.Element, new SelectionFilter(BuiltInCategory.OST_Rooms, true));
revitCommandEndedMonitor = new RevitCommandEndedMonitor(UiApplication);
revitCommandEndedMonitor.CommandEnded += OnCommandEnded;
Application.DocumentChanged += Application_DocumentChanged;
UiApplication.PostCommand(RevitCommandId.LookupPostableCommandId(PostableCommand.AutomaticCeiling));
}
catch (Autodesk.Revit.Exceptions.OperationCanceledException)
{
return;
}
}
private void Application_DocumentChanged(object sender, Autodesk.Revit.DB.Events.DocumentChangedEventArgs e)
{
AddedElement.AddRange(e.GetAddedElementIds());
}
private void OnCommandEnded(object sender, EventArgs e)
{
Application.DocumentChanged -= Application_DocumentChanged;
revitCommandEndedMonitor.CommandEnded -= OnCommandEnded;
foreach (var item in AddedElement)
{
var ele = Document.GetElement(item);
if (ele is Ceiling)
{
Ceillings.Add(ele.Id);
break;
}
}
using (TransactionGroup tranG = new TransactionGroup(Document, "Auto Create Ceilling"))
{
tranG.Start();
using (Transaction transs = new Transaction(Document, "test"))
{
transs.Start();
var opt = new SpatialElementBoundaryOptions();
for (int i = 1; i < selectRooms.Count; i++)
{
var tempCeilling = (Document.GetElement(Ceillings.FirstOrDefault())).Copy(XYZ.BasisY * 100 * i).FirstOrDefault();
Ceillings.Add(tempCeilling);
}
transs.Commit();
}
for (int i = 0; i < selectRooms.Count; i++)
{
ICollection<ElementId> ids;
List<Line> polygon = new List<Line>();
Room room = Document.GetElement(selectRooms[i]) as Room;
IList<IList<BoundarySegment>> loops = room.GetBoundarySegments(new SpatialElementBoundaryOptions());
foreach (IList<BoundarySegment> loop in loops)
{
foreach (BoundarySegment seg in loop)
{
Line line = seg.GetCurve() as Line;
polygon.Add(line);
}
}
using (Transaction transs = new Transaction(Document, "temp"))
{
transs.Start();
ids = Document.Delete(Ceillings[i]);
transs.RollBack();
}
using (Transaction trans = new Transaction(Document, "Change Sketch Ceilling"))
{
trans.Start();
EditCeilling(ids, polygon);
trans.Commit();
}
}
tranG.Assimilate();
}
}
private void EditCeilling(ICollection<ElementId> elementIds, List<Line> polygon)
{
List<ModelLine> detailLines = new List<ModelLine>();
foreach (ElementId id in elementIds)
{
Element ele = Document.GetElement(id);
if (ele is ModelLine)
{
detailLines.Add(ele as ModelLine);
}
}
var firstDetailLine = detailLines.FirstOrDefault();
foreach (var line in polygon)
{
Transform transform = Transform.CreateTranslation(new XYZ(0, 0, 1));
var tf = Transform.Identity;
var newID = ElementTransformUtils.CopyElement(Document, firstDetailLine.Id, XYZ.BasisY);
var newLine = Document.GetElement(newID.First()) as ModelLine;
var locCurve = newLine.Location as LocationCurve;
locCurve.Curve = Line.CreateBound(transform.OfPoint(line.GetEndPoint(0)), transform.OfPoint(line.GetEndPoint(1)));
}
Document.Delete(detailLines.Select(x => x.Id).ToList());
}
}
}
Loading

0 comments on commit 591760b

Please sign in to comment.