예제 #1
0
        public Result Execute(
            ExternalCommandData commandData,
            ref string message,
            ElementSet elements)
        {
            UIApplication app = commandData.Application;
            Document      doc = app.ActiveUIDocument.Document;

            if (!doc.IsFamilyDocument ||
                !doc.OwnerFamily.FamilyCategory.Name.Equals("Mass"))
            {
                message = "Please run this comand in a conceptual massing family document.";
                return(Result.Failed);
            }

            using (Transaction tx = new Transaction(doc))
            {
                tx.Start("Create Loft Form");

                FamilyItemFactory creator = doc.FamilyCreate;

                // Create profiles array
                ReferenceArrayArray ref_ar_ar = new ReferenceArrayArray();

                // Create first profile
                ReferenceArray ref_ar = new ReferenceArray();

                int           y     = 100;
                int           x     = 50;
                XYZ           pa    = new XYZ(-x, y, 0);
                XYZ           pb    = new XYZ(x, y, 0);
                XYZ           pc    = new XYZ(0, y + 10, 10);
                CurveByPoints curve = FormUtils.MakeCurve(creator, pa, pb, pc);
                ref_ar.Append(curve.GeometryCurve.Reference);
                ref_ar_ar.Append(ref_ar);

                // Create second profile
                ref_ar = new ReferenceArray();

                y     = 40;
                pa    = new XYZ(-x, y, 5);
                pb    = new XYZ(x, y, 5);
                pc    = new XYZ(0, y, 25);
                curve = FormUtils.MakeCurve(creator, pa, pb, pc);
                ref_ar.Append(curve.GeometryCurve.Reference);
                ref_ar_ar.Append(ref_ar);

                // Create third profile
                ref_ar = new ReferenceArray();

                y     = -20;
                pa    = new XYZ(-x, y, 0);
                pb    = new XYZ(x, y, 0);
                pc    = new XYZ(0, y, 15);
                curve = FormUtils.MakeCurve(creator, pa, pb, pc);
                ref_ar.Append(curve.GeometryCurve.Reference);
                ref_ar_ar.Append(ref_ar);

                // Create fourth profile
                ref_ar = new ReferenceArray();

                y     = -60;
                pa    = new XYZ(-x, y, 0);
                pb    = new XYZ(x, y, 0);
                pc    = new XYZ(0, y + 10, 20);
                curve = FormUtils.MakeCurve(creator, pa, pb, pc);
                ref_ar.Append(curve.GeometryCurve.Reference);
                ref_ar_ar.Append(ref_ar);

                Form form = creator.NewLoftForm(true, ref_ar_ar);
                tx.Commit();
            }
            return(Result.Succeeded);
        }
예제 #2
0
        //绘制模型线
        private void CreateCurve(XYZ startPoint, XYZ endPoint, XYZ normal1, XYZ normal2)
        {
            XYZ StartToEnd = new XYZ((endPoint - startPoint).X, (endPoint - startPoint).Y, 0);
            XYZ p_normal1  = new XYZ(normal1.X, normal1.Y, 0);
            XYZ p_normal2  = new XYZ(normal2.X, normal2.Y, 0);

            p_normal1 = p_normal1 / (Math.Sqrt(p_normal1.X * p_normal1.X + p_normal1.Y * p_normal1.Y));
            p_normal2 = p_normal2 / (Math.Sqrt(p_normal2.X * p_normal2.X + p_normal2.Y * p_normal2.Y));


            XYZ XoYprj_start = new XYZ(startPoint.X, startPoint.Y, 0);
            XYZ XoYprj_end   = new XYZ(endPoint.X, endPoint.Y, 0);

            //在起点、终点间插值,并在z=0平面上绘制NurbSpline曲线
            double[]    doubleArray    = { 1, 1, 1, 1, 1, 1 };
            IList <XYZ> controlPoints2 = new List <XYZ>();

            controlPoints2.Add(XoYprj_start);
            controlPoints2.Add(XoYprj_start + p_normal1 * mmToFeet(2000));
            controlPoints2.Add(XoYprj_start + p_normal1 * mmToFeet(4000));
            controlPoints2.Add(XoYprj_end + p_normal2 * mmToFeet(4000));
            controlPoints2.Add(XoYprj_end + p_normal2 * mmToFeet(2000));
            controlPoints2.Add(XoYprj_end);

            Curve nbLine = NurbSpline.Create(controlPoints2, doubleArray);


            //提取曲线上的拟合点,并在z轴方向插值拟合原曲线
            IList <XYZ> ptsOnCurve = nbLine.Tessellate();

            int ptCount = ptsOnCurve.Count;
            ReferencePointArray ptArr = new ReferencePointArray();

            for (int i = 0; i < ptCount; i++)
            {
                XYZ pt = ptsOnCurve[i];
                if (i < (ptCount - 1) / 8)
                {
                    ptArr.Append(m_familyCreator.NewReferencePoint(new XYZ(pt.X, pt.Y, startPoint.Z)));
                }
                else if (i > 7 * (ptCount - 1) / 8)
                {
                    ptArr.Append(m_familyCreator.NewReferencePoint(new XYZ(pt.X, pt.Y, endPoint.Z)));
                }
                else
                {
                    ptArr.Append(m_familyCreator.NewReferencePoint(new XYZ(pt.X, pt.Y, startPoint.Z + (i - (ptCount - 1) / 8) * (endPoint.Z - startPoint.Z) / (0.75 * (ptCount - 1)))));
                }
                //ReferencePoint p = m_familyCreator.NewReferencePoint(new XYZ(pt.X, pt.Y, startPoint.Z + i*(endPoint.Z - startPoint.Z)/ (ptCount - 1)));
                //ptArr.Append(p);
            }

            CurveByPoints curve = m_familyCreator.NewCurveByPoints(ptArr);

            curve.Visible = false;

            //创建放样平面并加入参照数组中
            int step = 8;//取step个点进行拟合
            ReferenceArrayArray refArr = new ReferenceArrayArray();

            for (int i = 0; i <= step; i++)
            {
                int position = i * (ptCount - 1) / step;

                //取起点截面及第二个截面作为参照平面
                if (i == 0)
                {
                    refArr.Append(CreatePlaneByPoint(ptArr.get_Item(position), ptArr.get_Item((i + 1) * (ptCount - 1) / step), (curve.GeometryCurve as HermiteSpline).Tangents[position], (curve.GeometryCurve as HermiteSpline).Tangents[((i + 1) * (ptCount - 1) / step)]));
                }
                //取终点截面及倒数第二个截面作为参照平面
                else if (i == step)
                {
                    refArr.Append(CreatePlaneByPoint(ptArr.get_Item(position), ptArr.get_Item((i - 1) * (ptCount - 1) / step), (curve.GeometryCurve as HermiteSpline).Tangents[position], (curve.GeometryCurve as HermiteSpline).Tangents[((i - 1) * (ptCount - 1) / step)]));
                }
                else
                {
                    refArr.Append(CreatePlaneByPoint(ptArr.get_Item(position), (curve.GeometryCurve as HermiteSpline).Tangents[position]));
                }
            }

            //创建放样实体
            m_familyCreator.NewLoftForm(true, refArr);
        }