找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 928|回复: 0

[分享] Ellipsoid Solid

[复制链接]

已领礼包: 859个

财富等级: 财运亨通

发表于 2014-6-7 01:17:52 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×


using System;
using System.IO;
using System.Collections.Generic;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Windows;


[CommandMethod("Ellipsoid")]
        //Basic method to put an ellipsoid at WCS origin
        static public void OpInitEl()
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            PromptDoubleOptions pdo = new PromptDoubleOptions("\nEnter Length ");
            pdo.AllowArbitraryInput = true;
            pdo.AllowNegative = false;
            PromptDoubleResult pdr = ed.GetDouble(pdo);
            if (pdr.Status != PromptStatus.OK || pdr.Value <= 0) return;
            double Length = pdr.Value;

            pdo.Message = " Enter Width";
            pdr = ed.GetDouble(pdo);
            if (pdr.Status != PromptStatus.OK || pdr.Value <= 0) return;
            double Width = pdr.Value;

            pdo.Message = " Enter Height";
            pdr = ed.GetDouble(pdo);
            if (pdr.Status != PromptStatus.OK || pdr.Value <= 0) return;
            double Height = pdr.Value;

            Double[] matarr = new Double[16]{Length/2, 0.0,0.0,0.0,
                                                0.0,Width/2,0.0,0.0,
                                                0.0,0.0,Height/2,0.0,
                                                0.0,0.0,0.0,1.0};

            Matrix3d mat = new Matrix3d(matarr);

            Point3dCollection Cvs = GenCVs();

            //Scale points from sphere to ellipsoid
            for (int i = 0; i < 9; i++ )
            {
                Cvs[i] = Cvs[i].TransformBy(mat);  
            }
            DoubleCollection Dc = GenWghts();
            KnotCollection[] Knots = GenKnots();
            Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                BlockTableRecord btr = (BlockTableRecord)(trans.GetObject(db.CurrentSpaceId, OpenMode.ForWrite));
                try
                {
                    //Generate the ellipsoid Octant
                    Autodesk.AutoCAD.DatabaseServices.NurbSurface NS = new Autodesk.AutoCAD.DatabaseServices.NurbSurface
                        (2, 2, true, 3, 3, Cvs, Dc,
                        Knots[0], Knots[1]);

                    //Create, mirror and union
                    Solid3d sol = new Solid3d();
                    sol.CreateBox(Length, Width, Height);
                    sol.TransformBy(Matrix3d.Scaling(0.5, new Point3d(Length / 2.0, Width / 2.0, Height / 2.0)));
                    sol.Slice(NS);

                    NS.Dispose();//Perhaps not specifically necessary - as NS is transaction resident
                    Plane Mir = new Plane(new Point3d(), new Vector3d(1.0,0.0,0.0));
                    Solid3d solMirr = sol.GetTransformedCopy(Matrix3d.Mirroring(Mir)) as Solid3d;
                    sol.BooleanOperation(BooleanOperationType.BoolUnite, solMirr);

                    Mir = new Plane(new Point3d(), new Vector3d(0.0, 1.0, 0.0));
                    solMirr = sol.GetTransformedCopy(Matrix3d.Mirroring(Mir)) as Solid3d;
                    sol.BooleanOperation(BooleanOperationType.BoolUnite, solMirr);

                    Mir = new Plane(new Point3d(), new Vector3d(0.0, 0.0, 1.0));
                    solMirr = sol.GetTransformedCopy(Matrix3d.Mirroring(Mir)) as Solid3d;
                    sol.BooleanOperation(BooleanOperationType.BoolUnite, solMirr);

                    sol.SetDatabaseDefaults();
                    btr.AppendEntity(sol);
                    trans.AddNewlyCreatedDBObject(sol, true);
                    trans.Commit();
                }
                catch
                {
                    trans.Abort();
                }
            }
        }

        private static Point3dCollection GenCVs()
        {
            //Generate Control Vertices for one octant of nurb based sphere
            Point3dCollection tempPts = new Point3dCollection();
            tempPts.Add(new Point3d(1.0, 0.0, 0.0));
            tempPts.Add(new Point3d(1.0, 0.0, 1.0));
            tempPts.Add(new Point3d(0.0, 0.0, 1.0));

            tempPts.Add(new Point3d(1.0, 1.0, 0.0));
            tempPts.Add(new Point3d(1.0, 1.0, 1.0));
            tempPts.Add(new Point3d(0.0, 0.0, 1.0));

            tempPts.Add(new Point3d(0.0, 1.0, 0.0));
            tempPts.Add(new Point3d(0.0, 1.0, 1.0));
            tempPts.Add(new Point3d(0.0, 0.0, 1.0));

            return tempPts;
        }

        private static DoubleCollection GenWghts()
        {
            //Generate weights for rational nurb surface
            DoubleCollection temp = new DoubleCollection(9);
            Double NearCorner = Math.Sqrt(0.5);

            temp.Add(1.0);
            temp.Add(NearCorner);
            temp.Add(1.0);

            temp.Add(NearCorner);
            temp.Add(0.5);
            temp.Add(NearCorner);

            temp.Add(1.0);
            temp.Add(NearCorner);
            temp.Add(1.0);

            return temp;
            
        }

        private static KnotCollection[] GenKnots()
        {
            //Generate knot vectors for PI based paramatization - uniform span from 0 to Pi/2 in U
            //                                                  - uniform span from 0 to Pi/2 in V
            KnotCollection[] tempKnots = new KnotCollection[2];

            tempKnots[0] = new KnotCollection();
            tempKnots[0].Add(0.0);
            tempKnots[0].Add(0.0);
            tempKnots[0].Add(0.0);

            tempKnots[0].Add(Math.PI / 2);
            tempKnots[0].Add(Math.PI / 2);
            tempKnots[0].Add(Math.PI / 2);

            tempKnots[1] = new KnotCollection();
            tempKnots[1].Add(0.0);
            tempKnots[1].Add(0.0);
            tempKnots[1].Add(0.0);

            tempKnots[1].Add(Math.PI / 2);
            tempKnots[1].Add(Math.PI / 2);
            tempKnots[1].Add(Math.PI / 2);

            return tempKnots;
        }

  
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-5-23 00:56 , Processed in 0.326548 second(s), 27 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表