Trung Tâm Revit Thực Hành NPD 4.4.5-Tham khảo lệnh hữu dụng 601/1 Lô A, CMT8, P15, Q10, TP.HCM 6,000,000VND/Khóa +84-913-875-375 Trung Tâm Revit Thực Hành NPD

Ths.KTS NGUYỄN PHƯỚC DỰ

Tài liệu học Revit, Tác giả : ThS.KTS.Nguyễn Phước Dự
Phần code này vì muốn để nguyên dạng cho các bạn dễ copy nên không thể Scale lại vừa thiết bị. Các bạn cần dùng máy tính để đọc trang này.
VIẾT LỆNH HỮU DỤNG
 
Chúng tôi giới thiệu một lệnh hữu dụng trong bộ công cụ của trung Tâm Revit thực hành NPD, dù lệnh đơn giản nhưng một đoạn Code cũng khá dài. Chúng ta cần kiên nhẫn đọc và thử từng đoạn để có thể tham khảo và rút kinh nghiệm dần. Chúng ta không có tham vọng viết ngay được những đoạn Code như thế này mà chỉ xem để có cái nhìn tổng thể và cóp nhặt vài thủ thuật, giải pháp của một dự án thực tế.
 
Đây là phần Code chính, còn vài file tiện ích và thư viện liên kết liên quan thì đoạn code mới chạy được. Chúng tôi giới thiệu phần này hy vọng trong quá trình viết Code sẽ có nhiều tình huống tương tự thì tham khảo. Vì thế chúng không cần phải Copy đoạn Code này vào chương trình.
 
Lệnh Spit Rebar là dùng để cắt cây thép Rebar ra làm hai hay nhiều đoạn. Trong mỗi đoạn cắt cần  kéo một tro ng hai đoạn thép vừa cắt giao lại với nhau đoạn dài nD do người dùng lựa chọn. Chúng ta cũng đưa ra lựa chọn có nhấn thép tại vị trí cắt hay không.
Tham khảo thêm lệnh SpitRebar tại đây.

 

using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using CElement = Autodesk.Revit.DB.Element;
using System.Windows.Forms;
using Autodesk.Revit.DB.Structure;
using Autodesk.Revit.UI.Selection;
// trong NameSpace này có một Class và nhiều thủ tục.

namespace DetailRebar

    [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    [Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]
    [Autodesk.Revit.Attributes.Journaling(Autodesk.Revit.Attributes.JournalingMode.NoCommandData)]
  // Class Command
public class SplitCommand : IExternalCommand
    {
        Document doc = null;  
 
        public Autodesk.Revit.UI.Result Execute(ExternalCommandData commandData, ref string message, Autodesk.Revit.DB.ElementSet elements)
        {
            ElementId Elemid = null;
            UIApplication uiApp = null; 
                      
            CElement m_rebarHost;
            Boolean DoiTuongFloor= false;
            IList curves = new List();
            IList curvesNghich = new List();
            IList curves2 = new List();
            Double GioThieu = Properties.Settings.Default.GioiThieu;
            GioThieu = GioThieu - Math.Round((GioThieu / 5)) * 5;
            RibbonPanel NPD_Option = null;
            if (GioThieu == 0)
            {
                MessageBox.Show("- Trung Tâm Revit thực hành NPD, \n nơi đào tạo REVIT ỨNG DỤNG cho Ngưới đi làm \n và cho Doanh nghiệp. \n https://hocrevit.com & http://trungtamrevit.com \n- Phiên bản miễn phí dành cho cá nhân. \n- Nếu là Doanh nghiệp, vui lòng đăng ký key để sử dụng.", "Trung Tâm Revit thực hành NPD");
               
            }
            Properties.Settings.Default.GioiThieu = GioThieu + 1;
            Transaction transaction = new Transaction(commandData.Application.ActiveUIDocument.Document, "Create Tool");
            Transaction trans_Delete = new Transaction(commandData.Application.ActiveUIDocument.Document, "Delete Tool");
            try
            {
                transaction.Start();
                uiApp = new UIApplication(commandData.Application.Application);
                doc = commandData.Application.ActiveUIDocument.Document;
                ISelectionFilter selFilter = new RBSelectionFilter();
                Reference elem = uiApp.ActiveUIDocument.Selection.PickObject(ObjectType.Element,selFilter, "Select a Rebar");
                Elemid = elem.ElementId;               
                Rebar Rb_Origin = (Rebar)doc.GetElement(Elemid);
                IList Rb_Linelist = Rb_Origin.GetCenterlineCurves(true,false,false, MultiplanarOption.IncludeOnlyPlanarCurves, 0);
 
                m_rebarHost = doc.GetElement(Rb_Origin.GetHostId());
                if (m_rebarHost.GetType().Name == "Floor")
                {
                    DoiTuongFloor = true;
                }
                //============================Carete WorkPlane             
 
                    //CurveLoop cl = CurveLoop.Create(Rb_Linelist);
                    Plane pl = Plane.CreateByNormalAndOrigin(Rb_Origin.Normal, Rb_Linelist[0].GetEndPoint(0));
                    SketchPlane skp = SketchPlane.Create(doc, pl);
                    doc.ActiveView.SketchPlane = skp;
                
                //============================
               
 
                RebarBarType barType = doc.GetElement(Rb_Origin.GetTypeId()) as RebarBarType;
                RebarShape barShape =  (RebarShape)doc.GetElement(Rb_Origin.RebarShapeId);
                RebarHookType hookType1 = doc.GetElement(Rb_Origin.GetHookTypeId(0)) as RebarHookType;
                RebarHookType hookType2 = doc.GetElement(Rb_Origin.GetHookTypeId(1)) as RebarHookType;
                            
               //===================Khoi tao Tuy chon
 
                List loadedPanels = uiApp.GetRibbonPanels("NPD-Util");
               
                foreach (RibbonPanel p in loadedPanels)
                {
                    if (p.Name.Equals("Option")) 
                    {
                        NPD_Option = p;
                        NPD_Option.Visible = true;
                        NPD_Option.Enabled = true;
                       
                    } 
                }
 
                 // Set tham so mac dinh
                 IList rb_Items = NPD_Option.GetItems();
                 Autodesk.Revit.UI.TextBox tbDaiChuan = ((Autodesk.Revit.UI.TextBox)rb_Items[0]);
                 tbDaiChuan.Value = Properties.Settings.Default.DaiThepChuan;
 
                 IList tg_Item = ((Autodesk.Revit.UI.ComboBox)rb_Items[1]).GetItems();
                if (Properties.Settings.Default.CatTuDo)
                            {                    
                                ((Autodesk.Revit.UI.ComboBox)rb_Items[1]).Current = tg_Item[0];
                            } else{
                                ((Autodesk.Revit.UI.ComboBox)rb_Items[1]).Current = tg_Item[1];
                            }
 
                
                //========================Cat ngang
                ObjectSnapTypes snapTypes =   ObjectSnapTypes.Nearest;
                
                XYZ Pt1 = uiApp.ActiveUIDocument.Selection.PickPoint(snapTypes, "Select point to Split");
                XYZ Pt2 = uiApp.ActiveUIDocument.Selection.PickPoint(snapTypes, "Select point to kip");
 
 
 
 
                 Properties.Settings.Default.DaiThepChuan = Convert.ToDouble( tbDaiChuan.Value);
                 Autodesk.Revit.UI.ComboBoxMember Rib_Item = ((Autodesk.Revit.UI.ComboBox)rb_Items[1]).Current;
 
                 if (Rib_Item.Name == "1")
                 {
 
                     Properties.Settings.Default.CatTuDo = true;
                     Properties.Settings.Default.Save();
                 }
                 else
                 {
                     Properties.Settings.Default.CatTuDo = false;
                     Properties.Settings.Default.Save();
                 }
 
               
                //=======Tim Huong cat
                 if (hookType1 != null)
                 {
                     Rb_Linelist.RemoveAt(0);
                     Rb_Linelist.RemoveAt(0);
                 }
                 if (hookType2 != null)
                 {
                     Rb_Linelist.RemoveAt(Rb_Linelist.Count - 1);
                     Rb_Linelist.RemoveAt(Rb_Linelist.Count - 1);
                 }
                //=======================
                 int TimP2 = 0;
                 int DoanCat = 0;
                 for (int i = 0; i < Rb_Linelist.Count; i++)
                 {
                     int p1 = 0;
                     int p2 = 5;
                     Curve cvx = Rb_Linelist[i];
                     XYZ _px0 = cvx.GetEndPoint(0);
                     XYZ _px1 = cvx.GetEndPoint(0);
                     if (inLine(Pt1, Rb_Linelist[i]))
                     {
                         p1 = i;
                         TimP2 = 1;
                         DoanCat = i;
                     }
                     if (inLine(Pt2, Rb_Linelist[i]))
                     {
                          p2 = i;
                         TimP2 = 2;
                         
                     };
 
                     if (p1 == p2)
                     {
                         Curve CV_p1 = Line.CreateBound(cvx.GetEndPoint(0), Pt1);
                         if (inLine(Pt2, CV_p1))
                         {
                             TimP2 = 1;
                         }
                         else
                         {
                             TimP2 = 2;
                         }
                     }
 
                 }
                 bool HuongThuan = TimP2==1;   
                
 
                          
               bool DoituongTron = false; 
                double lenChuan = UnitNPD.ToINCH(Properties.Settings.Default.DaiThepChuan);
                //====Kiem tra doi tuong trong hay thang de xac dinh cach cat
                for (int i = 0; i < Rb_Linelist.Count; i++)
                {
                    if(Rb_Linelist.Count>3) break;
 
                    if (Rb_Linelist[i].GetType().Name == "Arc" && Rb_Linelist[i].Length > 1)
                    {
                        DoituongTron=true;
                        break;
                    }
                }
 
                //====Bat dau cat
 
                if (DoituongTron)
                {
                    CatThepArc(Rb_Origin, Rb_Linelist, Pt1, Pt2);
                }
                else
                {
 
                    CatThepLine(Rb_Origin, Rb_Linelist, Pt1, Pt2, HuongThuan, DoanCat);
                   
                   
                }
 
                //=============
 
            }
            catch (Exception e)
            {
                message = e.Message.ToString();
                return Autodesk.Revit.UI.Result.Cancelled;
            }
            finally
            {
                if (NPD_Option != null) NPD_Option.Visible = false;
                transaction.Commit();
            }
            return Autodesk.Revit.UI.Result.Succeeded;
        }
        void CatThepLine(Rebar Rb_Origin, IList Rb_Linelist, XYZ Pt1, XYZ Pt2, bool HuongThuan, int DoanCat)
        {
            int lineInt = 0;           
            
            Boolean DoiTuongFloor = false;
           
           
 
            CElement m_rebarHost = doc.GetElement(Rb_Origin.GetHostId());
 
            RebarBarType barType = doc.GetElement(Rb_Origin.GetTypeId()) as RebarBarType;
            RebarShape barShape = (RebarShape)doc.GetElement(Rb_Origin.RebarShapeId);
            Double D = barType.BarDiameter;
            Double RBend = barType.StandardBendDiameter;
            Double KCachOffset = 1.5 * RBend * Math.Pow(Math.Tan(3.14 / 8), 2) / (1 + Math.Pow(Math.Tan(3.14 / 8), 2));
            KCachOffset = (KCachOffset > (D * 1.25)) ? KCachOffset : (D * 1.25);
            //===================
            if (!Properties.Settings.Default.NhanThep_Nguoc) KCachOffset = -KCachOffset;
            Double DaiNoi = Properties.Settings.Default.DaiNoiThep;
            RebarHookType hookType1 = doc.GetElement(Rb_Origin.GetHookTypeId(0)) as RebarHookType;
            RebarHookType hookType2 = doc.GetElement(Rb_Origin.GetHookTypeId(1)) as RebarHookType;
            IList curves = new List();
            IList curves2 = new List();
            IList curvesNghich = new List();
            double lenghookType1=0;
            if (hookType1 != null) lenghookType1= barType.GetHookLength(hookType1.Id);
            double lenghookType2=0;
            if(hookType2!=null) lenghookType2= barType.GetHookLength(hookType2.Id);
if (HuongThuan) // Pt2 tren Doan dau
 {
   #region HuongThuan
TaoThepTiepLineTheo:
     double DaiTong = DenTong(Rb_Linelist);
  for (int i = 0; i < Rb_Linelist.Count; i++)
    {
        //================================= Phan 1 
        Curve cv = Rb_Linelist[i];
       
        double daiPhanTruoc = DenTong(curves) + cv.Length;
        double daiPhanSau = (DaiTong + cv.Length) - daiPhanTruoc;
        double DaiChuan = Properties.Settings.Default.DaiThepChuan / (12 * 25.4);
        //==================================
        if ((Properties.Settings.Default.CatTuDo && DoanCat == i) || (!Properties.Settings.Default.CatTuDo && daiPhanTruoc > DaiChuan)) // tim doan cat
                    {
                            //==================================
                                Line rebarLine = Line.CreateBound(cv.GetEndPoint(0), Pt1);
                                if (!Properties.Settings.Default.CatTuDo && daiPhanTruoc > DaiChuan) // cat chuan
                                {
 
                                    Pt1 = DenDaiChuan(curves, rebarLine, lenghookType1);
                                    rebarLine = Line.CreateBound(cv.GetEndPoint(0), Pt1);
                                    curves.Add(rebarLine);
                                    
                                }
                                else     // Cắt tự do
                                {                                   
                                   curves.Add(rebarLine);                                        
                                }
                        // Tao thep doan dau.
             CreateRebar(doc, m_rebarHost, barType, hookType1, null, curves, Rb_Origin.Normal, Rb_Origin);
             curves.Clear();
                        //============ Xu Ly noi thep cho doan sau
                                if (Properties.Settings.Default.NhanThep && !DoiTuongFloor)
                                {
 
 
                                    XYZ Ptx1 = GeomUtil.OffsetPoint(Pt1, GeomUtil.SubXYZ(cv.GetEndPoint(0), cv.GetEndPoint(1)), DaiNoi * D);
                                    Plane plt = Plane.CreateByThreePoints(Rb_Origin.Normal, GeomUtil.SubXYZ(cv.GetEndPoint(1), cv.GetEndPoint(0)), cv.GetEndPoint(0));
                                    Ptx1 = GeomUtil.OffsetPoint(Ptx1, plt.Normal, KCachOffset);
 
                                    XYZ Ptx2 = GeomUtil.OffsetPoint(Pt1, plt.Normal, KCachOffset);
                                    XYZ Ptx3 = GeomUtil.OffsetPoint(Pt1, GeomUtil.SubXYZ(cv.GetEndPoint(0), cv.GetEndPoint(1)), -Math.Abs(KCachOffset) * 1.732);
 
                                    Line rebarLine1 = Line.CreateBound(Ptx1, Ptx2);
                                    Line rebarLine2 = Line.CreateBound(Ptx2, Ptx3);
                                    rebarLine = Line.CreateBound(Ptx3, cv.GetEndPoint(1));
                                    Pt1 = GeomUtil.OffsetPoint(cv.GetEndPoint(1), GeomUtil.SubXYZ(cv.GetEndPoint(0), cv.GetEndPoint(1)), -DaiNoi * D); //cho Pt1 moi
                                   
                                    curves2.Add(rebarLine1);
                                    curves2.Add(rebarLine2); // doan xien
                                    curves2.Add(rebarLine);
                                }
                                else
                                {
                                   
                                    XYZ Ptx1 = GeomUtil.OffsetPoint(Pt1, GeomUtil.SubXYZ(Pt1, cv.GetEndPoint(1)), DaiNoi * D);                                   
                                    XYZ Ptx2 = cv.GetEndPoint(1);
                                    rebarLine = Line.CreateBound(Ptx1, Ptx2);
                                    curves2.Add(rebarLine);
                                }
                           
                }
                else // Khong xu ly, lay doan dang doc
                {
                    curves.Add(Rb_Linelist[i]);
                    lineInt += 1;
                    continue; // Doc doan tiep theo
                }
       //===========Xử lý phần còn lại.
      
                for (int j = i + 1; j < Rb_Linelist.Count; j++)
                {
                curves2.Add(Rb_Linelist[j]);
                }       
                                double DaiConLai = 0;
                                for (int m = 0; m < curves2.Count; m++)
                                {
                                    DaiConLai += curves2[m].Length;
                                }
                                if (!Properties.Settings.Default.CatTuDo && Properties.Settings.Default.CatAll)
                                {
                                    if (DaiConLai > Properties.Settings.Default.DaiThepChuan / (12 * 25.4))
                                    {
                                        Rb_Linelist.Clear();
                                        for (int m = 0; m < curves2.Count; m++)
                                        {
                                            Rb_Linelist.Add(curves2[m]);
 
                                        }
                                        curves2.Clear();
                                        lenghookType1 = 0;
                                        hookType1 = null;
                                        goto TaoThepTiepLineTheo; // Ra khoi vong For va laplai
                                       
                                    }
                                }
                                //=================
     CreateRebar(doc, m_rebarHost, barType, null, hookType2, curves2, Rb_Origin.Normal, Rb_Origin);
 
    //Xoa thanh thep goc
    doc.Delete(Rb_Origin.Id);
    break;
 } //for
   #endregion
}
else
{
    #region HuongNghich
TaoThepTiep_HuongNghich:
    double DaiTong = DenTong(Rb_Linelist);
for (int i = 0; i < Rb_Linelist.Count; i++)
{
    //================================= Phan 1 
    Curve cv = Rb_Linelist[i];
    double daiPhanTruoc = DenTong(curves) + cv.Length;
    double daiPhanSau = DaiTong  - daiPhanTruoc;
    double DaiChuan = Properties.Settings.Default.DaiThepChuan / (12 * 25.4);
    //==================================
    if ((Properties.Settings.Default.CatTuDo && DoanCat == i) || (!Properties.Settings.Default.CatTuDo && daiPhanSau < DaiChuan)) // tim doan cat
    {
                                    //==================================
                                
                                for (int j = i + 1; j < Rb_Linelist.Count; j++)
                                {
                                    curvesNghich.Add(Rb_Linelist[j]);
                                }
                                Line rebarLine = Line.CreateBound( Pt1,cv.GetEndPoint(1));
                                if (!Properties.Settings.Default.CatTuDo && daiPhanSau < DaiChuan)
                                {
                                    
                                    // rebarLine = Line.CreateBound(cv.GetEndPoint(1), Pt1);
                                    if (curvesNghich.Count > 0)
                                    {
                                        Pt1 = DenDaiChuanNghich(curvesNghich, cv, lenghookType2);
                                    }
                                    else
                                    {
                                        Pt1 = GeomUtil.OffsetPoint(cv.GetEndPoint(1), GeomUtil.SubXYZ(cv.GetEndPoint(0), cv.GetEndPoint(1)), (DaiChuan - lenghookType2));
                                    }
                                    rebarLine = Line.CreateBound(Pt1, cv.GetEndPoint(1));
                                    curvesNghich.Insert(0,rebarLine);
 
                                }
                                else     // Cắt tự do
                                {
                                    curvesNghich.Insert(0, rebarLine);
                                }
         // Tao thep doan sau.
       
                                CreateRebar(doc, m_rebarHost, barType, null, hookType2, curvesNghich, Rb_Origin.Normal, Rb_Origin);
           curvesNghich.Clear();
        // =============================================================xu ly truoc
                    if (Properties.Settings.Default.NhanThep && !DoiTuongFloor)
                    {
 
                        XYZ Ptx1 = GeomUtil.OffsetPoint(Pt1, GeomUtil.SubXYZ(cv.GetEndPoint(1), cv.GetEndPoint(0)), DaiNoi * D);
                        Plane plt = Plane.CreateByThreePoints(Rb_Origin.Normal, GeomUtil.SubXYZ(cv.GetEndPoint(1), cv.GetEndPoint(0)), cv.GetEndPoint(0));
                        Ptx1 = GeomUtil.OffsetPoint(Ptx1, plt.Normal, KCachOffset);
                        XYZ Ptx2 = GeomUtil.OffsetPoint(Pt1, plt.Normal, KCachOffset);
                        XYZ Ptx3 = GeomUtil.OffsetPoint(Pt1, GeomUtil.SubXYZ(cv.GetEndPoint(0), cv.GetEndPoint(1)), Math.Abs(KCachOffset) * 1.732);
 
                           
                        rebarLine = Line.CreateBound(cv.GetEndPoint(0), Ptx3);
                        Line rebarLine2 = Line.CreateBound(Ptx3, Ptx2);
                        Line rebarLine1 = Line.CreateBound(Ptx2, Ptx1);
 
                        Pt1 = GeomUtil.OffsetPoint(cv.GetEndPoint(0), GeomUtil.SubXYZ(cv.GetEndPoint(1), cv.GetEndPoint(0)), DaiNoi * D); //cho Pt1 moi
 
                        curves.Add( rebarLine);
                        curves.Add( rebarLine2);
                        curves.Add( rebarLine1);
                    }
                    else
                    {
                        KCachOffset = 0;
                        XYZ Ptx1 = GeomUtil.OffsetPoint(Pt1, GeomUtil.SubXYZ(Pt1, cv.GetEndPoint(1)), -DaiNoi * D);
                        rebarLine = Line.CreateBound(cv.GetEndPoint(0), Ptx1);
                        curves.Add(rebarLine);
 
                    }
    //=============
}
else // lay doan hien tai, khong xu ly
{
    curves.Add(Rb_Linelist[i]);
    lineInt += 1;
    continue; // Doc doan tiep theo
}
    //===========Xử lý phần còn lại. 
                            
 
                
                                double DaiConLai = 0;
 
                                for (int m = 0; m < curves.Count; m++)
                                {
                                    DaiConLai += curves[m].Length;
                                }
 
                                if (!Properties.Settings.Default.CatTuDo && Properties.Settings.Default.CatAll)
                                {
                                    if (DaiConLai > Properties.Settings.Default.DaiThepChuan / (12 * 25.4))
                                    {
                                        Rb_Linelist.Clear();
                                        for (int m = 0; m < curves.Count; m++)
                                        {
                                            Rb_Linelist.Add(curves[m]);
 
                                        }
                                        curves.Clear();
                                        lenghookType2=0;
                                        hookType2 = null;
                                        goto TaoThepTiep_HuongNghich;
                                       
                                    }
                                }
                                //=================
                                CreateRebar(doc, m_rebarHost, barType, hookType1, null, curves, Rb_Origin.Normal, Rb_Origin);
 
                                //Xoa thanh thep goc
 
                                doc.Delete(Rb_Origin.Id);
                                break;
                } //for
#endregion
            } // if
}
        
       
 
                       
 
                       
 
        
void CatThepArc(Rebar Rb_Origin, IList Rb_Linelist, XYZ Pt1, XYZ Pt2)
        {
            int lineInt = 0;
            bool CatLienTuc = false;
            CElement m_rebarHost = doc.GetElement(Rb_Origin.GetHostId());
           
            RebarBarType barType = doc.GetElement(Rb_Origin.GetTypeId()) as RebarBarType;
            RebarShape barShape = (RebarShape)doc.GetElement(Rb_Origin.RebarShapeId);
            Double D = barType.BarDiameter;
            Double RBend = barType.StandardBendDiameter;
            Double KCachOffset = 1.5 * RBend * Math.Pow(Math.Tan(3.14 / 8), 2) / (1 + Math.Pow(Math.Tan(3.14 / 8), 2));
            KCachOffset = (KCachOffset > (D * 1.25)) ? KCachOffset : (D * 1.25);
            //KCachOffset = -KCachOffset; //Huong vao trong  if (!Properties.Settings.Default.NhanThep_Nguoc) KCachOffset = -KCachOffset;
            if (!Properties.Settings.Default.NhanThep_Nguoc) KCachOffset = -KCachOffset;
            Double DaiNoi = Properties.Settings.Default.DaiNoiThep;
            RebarHookType hookType1 = doc.GetElement(Rb_Origin.GetHookTypeId(0)) as RebarHookType;
            RebarHookType hookType2 = doc.GetElement(Rb_Origin.GetHookTypeId(1)) as RebarHookType;
            IList curves = new List();           
            IList curves2 = new List();
            for (int i = 0; i < Rb_Linelist.Count; i++)
            {
                #region DoiTuongArc
 
                if (Rb_Linelist[i].GetType().Name == "Arc" && Rb_Linelist[i].Length > 1)
                {
                    Arc Acv2 = (Arc)Rb_Linelist[i];
                    Boolean First = true;
 
                TaoThepTiepTheo:
 
                    XYZ St_pt = Acv2.GetEndPoint(0);
                    XYZ end_pt = Acv2.GetEndPoint(1);
                    XYZ cen_pt = Acv2.Center;
                    double banKinh = Acv2.Radius;
 
                    XYZ St_Vto_pt = GeomUtil.SubXYZ(St_pt, cen_pt);
                    XYZ end_Vto_pt = GeomUtil.SubXYZ(end_pt, cen_pt);
                    //========
                    XYZ Pt2_Vto = GeomUtil.SubXYZ(Pt2, cen_pt);
                    double Goc_pt2 = GeomUtil.Angle2VT(Pt2_Vto, Acv2.XDirection);
 
                    XYZ Pt1_Vto = GeomUtil.SubXYZ(Pt1, cen_pt);
                    double Goc_pt1 = GeomUtil.Angle2VT(Pt1_Vto, Acv2.XDirection);
                    //=============Xac đinh huong
                    Boolean HuongChuan = true;
                    double lenChuan = UnitNPD.ToINCH(Properties.Settings.Default.DaiThepChuan);
 
                    if (First)
                    {
                        HuongChuan = GeomUtil.HuongChuan(St_Vto_pt, end_Vto_pt, Pt1_Vto);
                        if (!HuongChuan)
                        {
                            St_pt = Acv2.GetEndPoint(1);
                            end_pt = Acv2.GetEndPoint(0);
                            end_Vto_pt = GeomUtil.SubXYZ(end_pt, cen_pt);
                            St_Vto_pt = GeomUtil.SubXYZ(St_pt, cen_pt);
                            Acv2 = Arc.Create(St_pt, end_pt, Pt1);
                        }
                    }
 
                    if (CatLienTuc || inArc(Pt1, Acv2)) //Kiem diem P1
                    {
                        //==========KiemTra Diem pt2                     
 
                        Boolean HuongCung = GeomUtil.HuongChuan(St_Vto_pt, Pt1_Vto, Pt2_Vto);
 
                        if (HuongCung) // truong hop Pt1 o phan dau
                        {
 
                            XYZ pt_Split = Pt1;
                            double GocQuay = 0;
                            if (!Properties.Settings.Default.CatTuDo)
                            {
                                pt_Split = DenDaiCungChuan(St_pt, Acv2, lenChuan);
 
                            }
 
 
 
                            double St_VT = GeomUtil.AngleToGoc(GeomUtil.SubXYZ(St_pt, cen_pt)) - GeomUtil.AngleToGoc(Acv2.XDirection);
                            double End_VT = GeomUtil.AngleToGoc(GeomUtil.SubXYZ(pt_Split, cen_pt)) - GeomUtil.AngleToGoc(Acv2.XDirection);
 
                            St_VT = St_VT >= -0.001 ? St_VT : 2 * Math.PI + St_VT;      // goc lon hon 180  
                            End_VT = End_VT >= -0.001 ? End_VT : 2 * Math.PI + End_VT;      // goc lon hon 180  
 
                            Curve new_cv1 = Arc.Create(cen_pt, Acv2.Radius, St_VT, End_VT, Acv2.XDirection, Acv2.YDirection);
 
                            curves.Add(new_cv1);
                            //============================================================================================================== Xu ly doan con lai
 
                            XYZ pt_Noi = DenDaiCungChuan(pt_Split, (Arc)new_cv1, -DaiNoi * D);
 
                            double Start_VT2 = GeomUtil.AngleToGoc(GeomUtil.SubXYZ(pt_Noi, cen_pt)) - GeomUtil.AngleToGoc(Acv2.XDirection);
 
 
                            //==============================================================================================================
                            double End_VT2 = GeomUtil.AngleToGoc(GeomUtil.SubXYZ(end_pt, Acv2.Center)) - GeomUtil.AngleToGoc(Acv2.XDirection);
 
                            End_VT2 = End_VT2 >= -0.001 ? End_VT2 : 2 * Math.PI + End_VT2; // goc lon hon 180
                            Start_VT2 = Start_VT2 >= -0.001 ? Start_VT2 : 2 * Math.PI + Start_VT2;  // goc lon hon 180
 
                            Curve new_cv2 = Arc.Create(Acv2.Center, Acv2.Radius, Start_VT2, End_VT2, Acv2.XDirection, Acv2.YDirection);
 
 
 
                            CreateRebar(doc, m_rebarHost, barType, hookType1, null, curves, Rb_Origin.Normal, Rb_Origin);
                            curves.Clear();
                            if (!Properties.Settings.Default.CatTuDo)
                            {
 
                                if (new_cv2.Length > lenChuan && Properties.Settings.Default.CatAll)
                                {
                                    Acv2 = (Arc)new_cv2;
                                    CatLienTuc = true;
                                    First = false;
                                    Pt2 = new_cv1.GetEndPoint(1);
                                    Pt1 = DenDaiCungChuan(Pt2, (Arc)new_cv1, DaiNoi * D);
                                    goto TaoThepTiepTheo;
                                }
                            }
 
                            // ==Tai tao phan 2
 
                            curves2.Add(new_cv2);
                            CreateRebar(doc, m_rebarHost, barType, null, hookType2, curves2, Rb_Origin.Normal, Rb_Origin);
                        }
                        //==============================
                        else //===================================================== truong hop Pt2 phan sau
                        {
                            XYZ pt_Split = Pt1;
                            double GocQuay = 0;
                            if (!Properties.Settings.Default.CatTuDo)
                            {
                                pt_Split = DenDaiCungChuan(end_pt, Acv2, -lenChuan);
 
                            }
 
 
                            double Split_VT = GeomUtil.AngleToGoc(GeomUtil.SubXYZ(pt_Split, cen_pt)) - GeomUtil.AngleToGoc(Acv2.XDirection);
                            double End_VT = GeomUtil.AngleToGoc(GeomUtil.SubXYZ(end_pt, cen_pt)) - GeomUtil.AngleToGoc(Acv2.XDirection);
                            Split_VT = Split_VT >= -0.001 ? Split_VT : 2 * Math.PI + Split_VT; // goc lon hon 180
                            End_VT = End_VT >= -0.001 ? End_VT : 2 * Math.PI + End_VT;  // goc lon hon 180
 
                            Curve new_cv1 = Arc.Create(cen_pt, Acv2.Radius, Split_VT, End_VT, Acv2.XDirection, Acv2.YDirection);
 
                            //=========Xac dinh doan dau                           
                            curves.Add(new_cv1);
                            //============================================================================================================== Xu ly doan con lai
 
                            XYZ pt_Noi = DenDaiCungChuan(pt_Split, (Arc)new_cv1, DaiNoi * D);
                            double End_VT2 = GeomUtil.AngleToGoc(GeomUtil.SubXYZ(pt_Noi, Acv2.Center)) - GeomUtil.AngleToGoc(Acv2.XDirection);
                            End_VT2 = End_VT2 >= -0.001 ? End_VT2 : 2 * Math.PI + End_VT2;  // goc lon hon 180
 
                            Curve new_cv2 = Arc.Create(Acv2.Center, Acv2.Radius, 0, End_VT2, Acv2.XDirection, Acv2.YDirection);
 
 
 
                            CreateRebar(doc, m_rebarHost, barType, hookType1, null, curves, Rb_Origin.Normal, Rb_Origin);
                            curves.Clear();
                            if (!Properties.Settings.Default.CatTuDo)
                            {
 
                                if (new_cv2.Length > lenChuan && Properties.Settings.Default.CatAll)
                                {
                                    Acv2 = (Arc)new_cv2;
                                    CatLienTuc = true;
                                    First = false;
                                    Pt2 = new_cv1.GetEndPoint(1);
                                    Pt1 = DenDaiCungChuan(Pt2, (Arc)new_cv1, -DaiNoi * D);
                                    goto TaoThepTiepTheo;
                                }
                            }
 
                            // ==Tai tao phan 2
 
                            curves2.Add(new_cv2);
                            CreateRebar(doc, m_rebarHost, barType, null, hookType2, curves2, Rb_Origin.Normal, Rb_Origin);
                            //==============================
                           
                        }
                        //Xoa thanh thep goc
 
                        doc.Delete(Rb_Origin.Id);
                        break;
                    }
                    else
                    {
                        curves.Add(Rb_Linelist[i]);
                        lineInt += 1;
                    }
                }
                #endregion
            }
            
        }

        Rebar CreateRebar(Autodesk.Revit.DB.Document document, CElement m_rebarHost, RebarBarType barType, RebarHookType hookType1, RebarHookType hookType2, IList curves, XYZ Normal, Rebar Rb_Origin)

        {              
            //===============================
            Rebar rebar = Rebar.CreateFromCurves(document, Autodesk.Revit.DB.Structure.RebarStyle.Standard, barType, hookType1, hookType2,
                                m_rebarHost, Normal, curves, RebarHookOrientation.Right, RebarHookOrientation.Left, true, true);
 
            if (null != rebar)
            {
              
                 RebarLayoutRule LayOutRule = Rb_Origin.LayoutRule;
                switch (LayOutRule)
                {
                    case RebarLayoutRule.FixedNumber:
                        rebar.SetLayoutAsFixedNumber(Rb_Origin.NumberOfBarPositions, Rb_Origin.ArrayLength, Rb_Origin.BarsOnNormalSide, Rb_Origin.IncludeFirstBar, Rb_Origin.IncludeLastBar);
                    break;
                    case RebarLayoutRule.MaximumSpacing:
                    rebar.SetLayoutAsMaximumSpacing(Rb_Origin.MaxSpacing, Rb_Origin.ArrayLength, Rb_Origin.BarsOnNormalSide, Rb_Origin.IncludeFirstBar, Rb_Origin.IncludeLastBar);
                    break;
                    case RebarLayoutRule.MinimumClearSpacing:
                    rebar.SetLayoutAsMinimumClearSpacing(Rb_Origin.MaxSpacing, Rb_Origin.ArrayLength, Rb_Origin.BarsOnNormalSide, Rb_Origin.IncludeFirstBar, Rb_Origin.IncludeLastBar);
                    break;
                    case RebarLayoutRule.NumberWithSpacing:
                    rebar.SetLayoutAsNumberWithSpacing(Rb_Origin.NumberOfBarPositions, Rb_Origin.MaxSpacing, Rb_Origin.BarsOnNormalSide, Rb_Origin.IncludeFirstBar, Rb_Origin.IncludeLastBar);
                    break;
                    }
            }
 
            return rebar;
        }

        private XYZ DenDaiChuan(IList cvs,Curve cv, double Hookleng=0)

        {
            double lenTotal=0;
            double lenEnd=0;
           foreach (Curve _cv in cvs)
           {
               lenTotal+= _cv.Length;
           }
           lenEnd = lenTotal - (Properties.Settings.Default.DaiThepChuan / (12 * 25.4) - Hookleng);
 
           XYZ ptx = GeomUtil.OffsetPoint(cv.GetEndPoint(0), GeomUtil.SubXYZ(cv.GetEndPoint(0), cv.GetEndPoint(1)), lenEnd);
           return ptx;
        }

        private XYZ DenDaiChuanNghich(IList cvs, Curve cv, double Hookleng = 0)

        {
            double lenTotal = 0;
            double lenEnd = 0;
            foreach (Curve _cv in cvs)
            {
                lenTotal += _cv.Length;
            }
            lenEnd = (lenTotal + cv.Length) - (Properties.Settings.Default.DaiThepChuan / (12 * 25.4) - Hookleng);
 
            XYZ ptx = GeomUtil.OffsetPoint(cv.GetEndPoint(0), GeomUtil.SubXYZ(cv.GetEndPoint(1), cv.GetEndPoint(0)), lenEnd);
            return ptx;
        }

        private double DenTong(IList cvs)

        {
            double lenTotal = 0;            
            foreach (Curve _cv in cvs)
            {
                lenTotal += _cv.Length;
            }           
            return lenTotal;
        }

        private XYZ DenDaiCungChuan(XYZ Startpt, Arc _Arc, double lenChuan)

        {
            double NuaChuVi = _Arc.Radius*Math.PI;  
            double lenTotal = _Arc.Length;  
            double lenEnd = lenTotal - lenChuan;
 
            double GocChuan =  (lenChuan / _Arc.Radius) ;          
            //===================================================================================Xac dinh huong goc         
         
            XYZ axis = _Arc.Normal;
            Transform rotation = Transform.CreateRotationAtPoint(axis, GocChuan , _Arc.Center);
            //=====================================================================================================
            XYZ nextptx = rotation.OfPoint(Startpt);
 
            return nextptx;
        }
        

       private bool inLine (XYZ pt, Curve _Line)

            {
           XYZ v1=GeomUtil.SubXYZ( _Line.GetEndPoint(0), pt);
           XYZ v2=GeomUtil.SubXYZ(  _Line.GetEndPoint(1), pt );
           XYZ v3 = GeomUtil.SubXYZ(_Line.GetEndPoint(0),_Line.GetEndPoint(1));
           double l1 = GeomUtil.GetLength(v1);
           double l2 = GeomUtil.GetLength(v2);
           double l3 = GeomUtil.GetLength(v3);
 
           return ((l1 +l2) /l3) < 1.01;
            }

       private bool inArc(XYZ pt, Arc _Arc)

       {
           XYZ Vetor = GeomUtil.SubXYZ(pt,_Arc.Center );
            double DaiCung = _Arc.Length;           
           
            if (GeomUtil.IsEqual(_Arc.GetEndPoint(0), pt) || GeomUtil.IsEqual(_Arc.GetEndPoint(1), pt)) return true;
 
            if (Math.Abs(Vetor.GetLength() - _Arc.Radius) < 0.001) // dk nam tren duong tron
            {
                XYZ St_pt = _Arc.GetEndPoint(0);
                XYZ end_pt = _Arc.GetEndPoint(1);
                XYZ cen_pt = _Arc.Center;
               
 
                XYZ St_Vto_pt = GeomUtil.SubXYZ(St_pt, cen_pt);
                XYZ end_Vto_pt = GeomUtil.SubXYZ(end_pt, cen_pt);
                XYZ Pt_Vto = GeomUtil.SubXYZ(pt, cen_pt);
 
                Arc _arc = Arc.Create(_Arc.GetEndPoint(0), _Arc.GetEndPoint(1), pt);
                XYZ St_pt1 = _arc.GetEndPoint(0);
                XYZ end_pt1 = _arc.GetEndPoint(1);
                XYZ cen_pt1 = _arc.Center;
                XYZ St_Vto_pt1 = GeomUtil.SubXYZ(St_pt1, cen_pt1);
                XYZ end_Vto_pt1 = GeomUtil.SubXYZ(end_pt1, cen_pt1);
 
                bool hCu = GeomUtil.HuongChuan(St_Vto_pt, end_Vto_pt, Pt_Vto);
                bool hMoi = GeomUtil.HuongChuan(St_Vto_pt1, end_Vto_pt1, Pt_Vto);
 
               if (Math.Abs(_arc.Length - DaiCung) < 0.001 && (hCu == hMoi))
                   
                {
                    return true;
                }
               
            }
            return false;
       }
      
      
    }
    
}
 

Sách học Revit, Tác giả : ThS.KTS.Nguyễn Phước Dự

(Nhấn Ctrl+V để Paste ảnh từ Clipboard)
Khách Đăng nhập