Client/Desktop/KMBIM3.0/23.10.16/Cmd/AvoidObstruction/FormAvoidObstruction.cs

389 lines
15 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using KDCS.Utils;
using KMBIM.Revit.Tools.Utils;
namespace KMBIM.Revit.Tools.AvoidObstruction
{
public partial class FormAvoidObstruction : System.Windows.Forms.Form
{
private List<KDCSIntersectorElement> m_ListObstructionElement = null; // 충돌요소
private List<KDCSIntersectorElement> m_ListAvoidanceElement = null; // 회피요소
private UIDocument m_rvtUIDoc;
private Document m_rvtDoc;
private Autodesk.Revit.ApplicationServices.Application m_rvtApp;
private EnumAvoidObstructionType m_AvoidObstructinType;
public Autodesk.Revit.UI.UIDocument uidoc { get; set; }
private KDCSRequestHandler m_Handler;
private ExternalEvent m_ExEvent;
public FormAvoidObstruction(UIApplication uiapp, ExternalEvent exEvent, KDCSRequestHandler handler)
{
m_rvtApp = uiapp.Application;
m_rvtUIDoc = uiapp.ActiveUIDocument;
m_rvtDoc = m_rvtUIDoc.Document;
InitializeComponent();
m_Handler = handler;
m_ExEvent = exEvent;
m_ListObstructionElement = new List<KDCSIntersectorElement>();
m_ListAvoidanceElement = new List<KDCSIntersectorElement>();
}
/// <summary>
/// Control enabler / disabler
/// </summary>
///
private void EnableCommands(bool status)
{
foreach (System.Windows.Forms.Control ctrl in this.Controls)
{
ctrl.Enabled = status;
}
if (!status)
{
//this.buttonCancel.Enabled = true;
}
}
// External Event Handler로부터 호출된다.
public void WakeUp()
{
EnableCommands(true);
}
// 충돌요소선택
private void buttonSelectObstructionElement_Click(object sender, EventArgs e)
{
// 선택한 패밀리요소로부터 파라미터를 복사합니다.
try
{
List<Type> lstSelType = new List<Type>();
lstSelType.Add(typeof(Duct));
lstSelType.Add(typeof(Pipe));
lstSelType.Add(typeof(CableTray));
lstSelType.Add(typeof(Conduit));
lstSelType.Add(typeof(FamilyInstance));
lstSelType.Add(typeof(RevitLinkInstance)); // 참조 구조->보
m_ListObstructionElement.Clear();
string msg = Properties.Resources.SelectObstructionElement;
IList<Reference> lstRef = null;
try
{
// ObjectType.LinkedElement
if (checkBoxLinkModel.Checked == true)
{
lstRef = m_rvtUIDoc.Selection.PickObjects(ObjectType.LinkedElement, new AvoidObstructionSelectionFilter(lstSelType), msg);
}
else
{
lstRef = m_rvtUIDoc.Selection.PickObjects(ObjectType.Element, new AvoidObstructionSelectionFilter(lstSelType), msg);
}
}
catch
{
return;
}
if (lstRef != null)
{
foreach (var r in lstRef.ToList())
{
Element e1 = m_rvtDoc.GetElement(r.ElementId);
if (e1 != null)
{
Document doc = m_rvtDoc;
if (checkBoxLinkModel.Checked == true)
{
RevitLinkInstance rli = e1 as RevitLinkInstance;
if (rli != null)
{
var link_doc = rli.GetLinkDocument();
e1 = link_doc.GetElement(r.LinkedElementId);
var its_elm = new KDCSIntersectorElement(e1, null, link_doc);
its_elm.SetRevitLinkInstance(rli);
m_ListObstructionElement.Add(its_elm);
}
}
else
{
m_ListObstructionElement.Add(new KDCSIntersectorElement(e1, null));
}
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
// 방해(충돌) 요소 선택(요소갯수)
buttonSelectObstructionElement.Text = string.Format("{0}({1})", Properties.Resources.BtnSelecObstructionElement, m_ListObstructionElement.Count());
}
// 회피요소 선택
private void buttonSelectAvoidElement_Click(object sender, EventArgs e)
{
// 선택한 패밀리요소로부터 파라미터를 복사합니다.
try
{
if (m_ListObstructionElement.Count < 1)
{
MessageBox.Show(Properties.Resources.FirstSelectInterferenceElement);
return;
}
m_ListAvoidanceElement.Clear();
buttonSelectAvoidElement.Text = string.Format("{0}({1})", Properties.Resources.BtnSelectAvoidanceElement, m_ListAvoidanceElement.Count());
string msg = Properties.Resources.SelectAvoidanceElement;
List<Type> lstSelType = new List<Type>();
lstSelType.Add(typeof(Duct));
lstSelType.Add(typeof(Pipe));
lstSelType.Add(typeof(CableTray));
lstSelType.Add(typeof(Conduit));
m_ListAvoidanceElement.Clear();
List<Reference> lstRef = new List<Reference>();
if (m_AvoidObstructinType == EnumAvoidObstructionType.Avoid_Up || m_AvoidObstructinType == EnumAvoidObstructionType.Avoid_Dn)
{
try
{
Reference r = null;
while ((r = m_rvtUIDoc.Selection.PickObject(ObjectType.Element, new AvoidObstructionSelectionFilter(lstSelType), Properties.Resources.SelectAvoidanceElement)) != null)
{
Element e1 = m_rvtDoc.GetElement(r.ElementId);
if (e1 != null)
{
var findAE = m_ListAvoidanceElement.Find(x => x.Id == e1.Id);
if (findAE == null) // 중복 선택 방지
{
m_ListAvoidanceElement.Add(new KDCSIntersectorElement(e1, new XYZ(r.GlobalPoint.X, r.GlobalPoint.Y, r.GlobalPoint.Z)));
}
}
r = null;
// 회피요소 선택(요소갯수)
buttonSelectAvoidElement.Text = string.Format("{0}({1})", Properties.Resources.BtnSelectAvoidanceElement, m_ListAvoidanceElement.Count());
}
}
catch (Autodesk.Revit.Exceptions.OperationCanceledException )
{
AvoidObstruction();
// 회피요소 선택(요소갯수)
//m_ListAvoidanceElement.Clear();
// buttonSelectAvoidElement.Text = string.Format("{0}({1})", Properties.Resources.BtnSelectAvoidanceElement, m_ListAvoidanceElement.Count());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return;
}
else
{
var ilist = m_rvtUIDoc.Selection.PickObjects(ObjectType.Element, new AvoidObstructionSelectionFilter(lstSelType), Properties.Resources.SelectAvoidanceElement);
foreach (var r1 in ilist) lstRef.Add(r1);
}
if (lstRef != null)
{
foreach (var r in lstRef.ToList())
{
Element e1 = m_rvtDoc.GetElement(r.ElementId);
if (e != null) m_ListAvoidanceElement.Add(new KDCSIntersectorElement(e1, r.GlobalPoint));
}
// 회피 기능 실행
try
{
// 방해(충돌) 요소 선택(요소갯수)
buttonSelectAvoidElement.Text = string.Format("{0}({1})", Properties.Resources.BtnSelectAvoidanceElement, m_ListAvoidanceElement.Count());
AvoidObstruction();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
//m_ListAvoidanceElement.Clear();
// 방해(충돌) 요소 선택(요소갯수)
//buttonSelectAvoidElement.Text = string.Format("{0}({1})", Properties.Resources.BtnSelectAvoidanceElement, m_ListAvoidanceElement.Count());
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void AvoidObstruction()
{
Resolver resolver = new Resolver(m_rvtDoc);
resolver.SetAvoidObstructionType(this.m_AvoidObstructinType);
if(Reg.getReg("Avoid_Angle")=="" || Reg.getReg("Avoid_Angle") == string.Empty)
resolver.UpDownAngle = Convert.ToDouble(numericUpDownAngle.Value);
else
resolver.UpDownAngle = Convert.ToDouble(Reg.getReg("Avoid_Angle"));
if (Reg.getReg("Avoid_Height")==""|| Reg.getReg("Avoid_Height") == string.Empty)
resolver.UpDownFreeSpaceHeight = Convert.ToDouble(numericUpDownFreeSpaceHeight.Value);
else
resolver.UpDownFreeSpaceHeight = Convert.ToDouble(Reg.getReg("Avoid_Height"));
resolver.SetElement(ref m_ListObstructionElement, ref m_ListAvoidanceElement);
m_Handler.Request.Make(resolver);
m_ExEvent.Raise();
EnableCommands(false);
}
private void radioButtonUpDn_CheckedChanged(object sender, EventArgs e)
{
m_AvoidObstructinType = EnumAvoidObstructionType.AvoidUpDn;
Reg.setReg("AvoidObstructinType", "1");
}
private void radioButton_Up_CheckedChanged(object sender, EventArgs e)
{
m_AvoidObstructinType = EnumAvoidObstructionType.Avoid_Up;
Reg.setReg("AvoidObstructinType", "2");
}
private void radioButton_Dn_CheckedChanged(object sender, EventArgs e)
{
m_AvoidObstructinType = EnumAvoidObstructionType.Avoid_Dn;
Reg.setReg("AvoidObstructinType", "3");
}
private void radioButtonDnUp_CheckedChanged(object sender, EventArgs e)
{
m_AvoidObstructinType = EnumAvoidObstructionType.AvoidDnUp;
Reg.setReg("AvoidObstructinType", "4");
}
private void radioButtonUp__CheckedChanged(object sender, EventArgs e)
{
m_AvoidObstructinType = EnumAvoidObstructionType.AvoidUp_;
Reg.setReg("AvoidObstructinType", "5");
}
private void radioButtonDn__CheckedChanged(object sender, EventArgs e)
{
m_AvoidObstructinType = EnumAvoidObstructionType.AvoidDn_;
Reg.setReg("AvoidObstructinType", "6");
}
private void FormAvoidObstruction_Load(object sender, EventArgs e)
{
this.KeyPreview = true;
string avoidObstructinType = Reg.getReg("AvoidObstructinType");
int nAvoidObstructinType = 1;
if(int.TryParse(avoidObstructinType, out nAvoidObstructinType))
m_AvoidObstructinType = (EnumAvoidObstructionType)nAvoidObstructinType;
else
m_AvoidObstructinType = EnumAvoidObstructionType.AvoidUpDn;
if(m_AvoidObstructinType == EnumAvoidObstructionType.AvoidUpDn) radioButtonUpDn.Checked = true;
else if (m_AvoidObstructinType == EnumAvoidObstructionType.Avoid_Up) radioButton_Up.Checked = true;
else if (m_AvoidObstructinType == EnumAvoidObstructionType.Avoid_Dn) radioButton_Dn.Checked = true;
else if (m_AvoidObstructinType == EnumAvoidObstructionType.AvoidDnUp) radioButtonDnUp.Checked = true;
else radioButtonUpDn.Checked = true;
string sCheckBoxLinkModel = Reg.getReg("checkBoxLinkModel");
checkBoxLinkModel.Checked = sCheckBoxLinkModel == "1" ? true : false;
//여유공간 높이
if (Reg.getReg("Avoid_Height") != "" || Reg.getReg("Avoid_Height") != string.Empty)
{
double hgt = Convert.ToDouble(Reg.getReg("Avoid_Height"));
numericUpDownFreeSpaceHeight.Value = (decimal)hgt;
}
//각도
if (Reg.getReg("Avoid_Angle") != "" || Reg.getReg("Avoid_Angle") != string.Empty)
{
double ang = Convert.ToInt32(Reg.getReg("Avoid_Angle"));
numericUpDownAngle.Value = (decimal)ang;
}
}
//여유공간 높이 값 변경
private void numericUpDownFreeSpaceHeight_ValueChanged(object sender, EventArgs e)
{
Reg.setReg("Avoid_Height", numericUpDownFreeSpaceHeight.Value.ToString());
}
//각도 값 변경
private void numericUpDownAngle_ValueChanged(object sender, EventArgs e)
{
Reg.setReg("Avoid_Angle", numericUpDownAngle.Value.ToString());
}
private void FormAvoidObstruction_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
this.DialogResult = DialogResult.Cancel;
}
else if (e.KeyCode == Keys.Enter)
{
this.DialogResult = DialogResult.OK;
}
}
private void checkBoxLinkModel_CheckedChanged(object sender, EventArgs e)
{
Reg.setReg("checkBoxLinkModel", checkBoxLinkModel.Checked?"1":"0");
}
}
}