304 lines
11 KiB
C#
304 lines
11 KiB
C#
using System.Collections.Generic;
|
|
using Autodesk.Revit.Attributes;
|
|
using Autodesk.Revit.DB;
|
|
using Autodesk.Revit.UI;
|
|
using KDCS.Utils;
|
|
using KMBIM.Revit.Tools.Properties;
|
|
using KMBIM.Revit.Tools.Cmd.Hanger;
|
|
using System.Windows.Forms;
|
|
using System;
|
|
using KMBIM.Revit.Tools.Utils;
|
|
using Autodesk.Revit.UI.Selection;
|
|
|
|
|
|
namespace KMBIM.Revit.Tools.Cmd.Hanger
|
|
{
|
|
|
|
/// <summary>
|
|
/// 행거 수동 배치
|
|
/// </summary>
|
|
[Regeneration(RegenerationOption.Manual)]
|
|
[Transaction(TransactionMode.Manual)]
|
|
public class PlaceHangerCommandManual : IExternalCommand
|
|
{
|
|
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
|
|
{
|
|
if (!WorkMain.GetInstance().IsValid) return Autodesk.Revit.UI.Result.Succeeded;
|
|
// Verify active document
|
|
if (null == commandData.Application.ActiveUIDocument.Document)
|
|
{
|
|
message = KMBIM.Revit.Tools.Properties.Resources.ActiveViewIsNull; // "현재 활성화된 뷰가 없습니다."; //"Active view is null.";
|
|
return Autodesk.Revit.UI.Result.Failed;
|
|
}
|
|
|
|
var uiapp = commandData.Application;
|
|
var uidoc = uiapp.ActiveUIDocument;
|
|
var app = uiapp.Application;
|
|
var doc = uidoc.Document;
|
|
var createDoc = uidoc.Document.Create;
|
|
|
|
//var lst=FamilyInstanceCreator.FindFamilyCandidates(doc, "libraries\\Hanger", "Hanger_Rec_Channel_C");
|
|
|
|
|
|
|
|
HangerSchema.SetAddInId(app.ActiveAddInId);
|
|
HangerSchema.Init();
|
|
|
|
|
|
List<MEPCurve> list = new List<MEPCurve>();
|
|
|
|
|
|
FormHangerPlacement frmHanger = new FormHangerPlacement(doc);
|
|
if (frmHanger.ShowDialog() != DialogResult.OK) return Autodesk.Revit.UI.Result.Succeeded;
|
|
SinglePlacement(ref commandData, ref message, ref frmHanger);
|
|
|
|
return Autodesk.Revit.UI.Result.Succeeded;
|
|
}
|
|
|
|
Result SinglePlacement(ref ExternalCommandData commandData, ref string message, ref FormHangerPlacement frmHanger)
|
|
{
|
|
|
|
|
|
var uiapp = commandData.Application;
|
|
var uidoc = uiapp.ActiveUIDocument;
|
|
var document = uidoc.Document;
|
|
var createDoc = uidoc.Document.Create;
|
|
|
|
|
|
// 설치 옵션
|
|
InstallOption installOption = new InstallOption("행거 배치-단독", DisplayUnit.METRIC);
|
|
installOption.AnchorInstallBase = frmHanger.AnchorInstallBase;
|
|
installOption.SetAnchorLevel(frmHanger.Level);
|
|
|
|
|
|
|
|
|
|
string hanger_family = frmHanger.PathFamily;
|
|
installOption.addOption(new UnitInstallOption(hanger_family, -1, frmHanger.HangerSpacing, frmHanger.DistanceFromJoint)); // 3.28084 ft = 1m
|
|
|
|
try
|
|
{
|
|
|
|
HangerFamily hangerFamily = new HangerFamily(installOption.GetAnchorLevel());
|
|
var newView3d = new View3DHanger(document);
|
|
hangerFamily.SetView3dHanger(newView3d);
|
|
HangerSettings settings = HangerSettings.GetSettings();
|
|
|
|
settings.InstallOptions = new List<InstallOption>();
|
|
settings.InstallOptions.Add(installOption);
|
|
|
|
Autodesk.Revit.UI.Selection.Selection selection = uidoc.Selection;
|
|
while (true)
|
|
{
|
|
Reference reference = null;
|
|
try
|
|
{
|
|
reference = selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.PointOnElement, new MEPCurveSelectionFilter(), "행거 위치 지정.");
|
|
if (reference == null)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
message = ex.Message;
|
|
break;
|
|
}
|
|
Autodesk.Revit.DB.MEPCurve mep_curve = document.GetElement(reference.ElementId) as Autodesk.Revit.DB.MEPCurve;
|
|
|
|
// 선택한 커브가 행거를 배치할 수 있는 커브인가?
|
|
if (!PlaceableCurve(mep_curve))
|
|
{
|
|
return Result.Succeeded;
|
|
}
|
|
|
|
Autodesk.Revit.DB.Transaction transaction = new Autodesk.Revit.DB.Transaction(document, "Place Hanger");
|
|
transaction.Start();
|
|
if (!hangerFamily.getSize((Autodesk.Revit.DB.Element)mep_curve, settings))
|
|
{
|
|
return Result.Succeeded;
|
|
}
|
|
|
|
var fs = HangerFamilySymbol.GetFamilysymbol(document, hangerFamily.GetKMBIMMEPCurveType(), hanger_family);
|
|
hangerFamily.SetFamilySymbol(fs);
|
|
if (hangerFamily.GetFamilySymbol() == null)
|
|
{
|
|
transaction.RollBack();
|
|
return Result.Failed;
|
|
}
|
|
|
|
// 행거 위치 및 앵커 Elevation
|
|
if (!hangerFamily.GetElevation(reference.GlobalPoint, installOption.AnchorInstallBase))
|
|
{
|
|
transaction.RollBack();
|
|
return Result.Failed;
|
|
}
|
|
if (!HangerSchema.CreateFamilyInstance(document, hangerFamily, installOption.AnchorInstallBase, ref message))
|
|
{
|
|
transaction.RollBack();
|
|
return Result.Failed;
|
|
}
|
|
document.Regenerate();
|
|
transaction.Commit();
|
|
}
|
|
return Result.Succeeded;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
message = ex.Message;
|
|
return Result.Failed;
|
|
}
|
|
|
|
|
|
return Autodesk.Revit.UI.Result.Succeeded;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 주어진 요소가 배치 가능한 곡선인가?
|
|
/// </summary>
|
|
/// <param name="element"></param>
|
|
/// <returns></returns>
|
|
private bool PlaceableCurve(Autodesk.Revit.DB.Element element)
|
|
{
|
|
if (element == null)
|
|
{
|
|
return false;
|
|
}
|
|
if (!(element is MEPCurve))
|
|
{
|
|
return false;
|
|
}
|
|
if (element is Autodesk.Revit.DB.Plumbing.FlexPipe)
|
|
{
|
|
return false;
|
|
}
|
|
if (element is Autodesk.Revit.DB.Mechanical.FlexDuct)
|
|
{
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 행거 자동 배치
|
|
/// </summary>
|
|
[Regeneration(RegenerationOption.Manual)]
|
|
[Transaction(TransactionMode.Manual)]
|
|
public class PlaceHangerCommandMulti : IExternalCommand
|
|
{
|
|
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
|
|
{
|
|
if (!WorkMain.GetInstance().IsValid) return Autodesk.Revit.UI.Result.Succeeded;
|
|
// Verify active document
|
|
if (null == commandData.Application.ActiveUIDocument.Document)
|
|
{
|
|
message = KMBIM.Revit.Tools.Properties.Resources.ActiveViewIsNull; // "현재 활성화된 뷰가 없습니다."; //"Active view is null.";
|
|
return Autodesk.Revit.UI.Result.Failed;
|
|
}
|
|
|
|
var uiapp = commandData.Application;
|
|
var uidoc = uiapp.ActiveUIDocument;
|
|
var app = uiapp.Application;
|
|
var doc = uidoc.Document;
|
|
var createDoc = uidoc.Document.Create;
|
|
|
|
|
|
HangerSchema.SetAddInId(app.ActiveAddInId);
|
|
HangerSchema.Init();
|
|
|
|
List<MEPCurve> list = new List<MEPCurve>();
|
|
|
|
var lstElement = Util.GetSelectedElements(uidoc);
|
|
if (lstElement.Count == 0)
|
|
{
|
|
TaskDialog.Show(Properties.Resources.Warning, "이 명령을 실행하기 전에 배치할 요소(덕트,파이프,케이블트레이)를 선택해야 합니다.");
|
|
return Autodesk.Revit.UI.Result.Succeeded;
|
|
}
|
|
else
|
|
{
|
|
foreach (var e in lstElement)
|
|
{
|
|
MEPCurve mc = e as MEPCurve;
|
|
if (mc != null) list.Add(mc);
|
|
}
|
|
|
|
}
|
|
|
|
if (list.Count > 0)
|
|
{
|
|
FormHangerPlacement frmHanger = new FormHangerPlacement(doc);
|
|
if (frmHanger.ShowDialog() != DialogResult.OK) return Autodesk.Revit.UI.Result.Succeeded;
|
|
MultiPlacement(ref commandData, ref message, ref list, ref frmHanger);
|
|
}
|
|
|
|
return Autodesk.Revit.UI.Result.Succeeded;
|
|
}
|
|
|
|
Result MultiPlacement(ref ExternalCommandData commandData, ref string message, ref List<MEPCurve> lst, ref FormHangerPlacement frmHanger)
|
|
{
|
|
var uiapp = commandData.Application;
|
|
var uidoc = uiapp.ActiveUIDocument;
|
|
var doc = uidoc.Document;
|
|
var createDoc = uidoc.Document.Create;
|
|
|
|
|
|
|
|
|
|
Autodesk.Revit.UI.Selection.Selection selection = uidoc.Selection;
|
|
|
|
// 배치 시작점 지정
|
|
Reference refStart = null;
|
|
try
|
|
{
|
|
refStart = selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.PointOnElement, new MEPCurveSelectionFilter(), "행거 배치 시작점 지정.");
|
|
if (refStart == null)
|
|
{
|
|
return Autodesk.Revit.UI.Result.Failed;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
message = ex.Message;
|
|
return Autodesk.Revit.UI.Result.Failed;
|
|
}
|
|
|
|
if (lst.Count > 0)
|
|
{
|
|
|
|
// 설치 옵션
|
|
InstallOption installOption = new InstallOption("행거 배치-다중", DisplayUnit.METRIC);
|
|
installOption.AnchorInstallBase = frmHanger.AnchorInstallBase;
|
|
installOption.SetAnchorLevel(frmHanger.Level);
|
|
|
|
|
|
string strFamily = frmHanger.PathFamily;
|
|
installOption.addOption(new UnitInstallOption(strFamily, -1, frmHanger.HangerSpacing, frmHanger.DistanceFromJoint)); // 3.28084 ft = 1m
|
|
|
|
Autodesk.Revit.DB.Transaction transaction = new Autodesk.Revit.DB.Transaction(doc, "행거 배치-다중");
|
|
transaction.Start();
|
|
string mainInstruction = "";
|
|
var automaticHangerPlacement = new AutomaticHangerPlacement(doc, lst);
|
|
if (!automaticHangerPlacement.Place(ref mainInstruction, installOption, refStart.GlobalPoint))
|
|
{
|
|
if (!string.IsNullOrEmpty(mainInstruction))
|
|
{
|
|
TaskDialog.Show(Properties.Resources.Error, mainInstruction);
|
|
}
|
|
transaction.RollBack();
|
|
}
|
|
else
|
|
{
|
|
transaction.Commit();
|
|
}
|
|
|
|
}
|
|
return Autodesk.Revit.UI.Result.Succeeded;
|
|
}
|
|
}
|
|
|
|
|
|
}
|