注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

信息 灵感 创新

I? =Information,Inspiration,Innovation

 
 
 

日志

 
 
关于我

we are 5. Mathematics, Computation, Programming, Engineering, and Making fun of life.

网易考拉推荐

折腾一下AutoCAD——四连杆的模拟运动  

2010-12-21 21:43:27|  分类: ObjectARX |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

没事瞎折腾,突然就想到了这个馊主意,试试四连杆的运动模拟,貌似还凑合,下面是代码,总感觉写的有点不精练:

using System;

using System.Collections.Generic;

using System.Text;

using System.Runtime.InteropServices;

using Autodesk.AutoCAD.Runtime;

using Autodesk.AutoCAD.Geometry;

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.EditorInput;

namespace CADTest

{

    public class Class1

    {

        double theta = 0;

        const double PI = Math.PI;

        const int n = 72;

        const double delt = PI / n;//这个时候每°刷新一次

        Point3d p1 = new Point3d(0, 0, 0);

        Point3d p2, p3, p4;//注意没有初始化

        const double L1 = 100;//曲柄长度

        const double L2 = 150;//连杆长度

        const double L3=300;//摇杆长度

        const double L4=300;//机架长度

        int i = 4*n;//转两圈就停下来

        [CommandMethod("test")]

        public void test()

        {

            if (i == 0)

            {

                i = 4*n;

                return;

            }

            Document doc = Application.DocumentManager.MdiActiveDocument;

            Database db = doc.Database;

            Editor ed = doc.Editor;

            //先清除图形

            using (Transaction trans = db.TransactionManager.StartTransaction())

            {

                PromptSelectionResult psr = ed.SelectAll();

                SelectionSet ss = psr.Value;

                if (ss != null)

                {

                    foreach (SelectedObject so in ss)

                    {

                        Entity ent = trans.GetObject(so.ObjectId, OpenMode.ForWrite) as Entity;

                        ent.Erase(true);

                    }

                }

                trans.Commit();

            }

            //清除完毕开始绘图

            using (Transaction trans = db.TransactionManager.StartTransaction())

            {

                BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;

                BlockTableRecord btr = trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

                p2 = new Point3d(L1 * Math.Cos(theta), L1 * Math.Sin(theta), 0);

                p4 = new Point3d(300, 0, 0);

                Line MyL1 = new Line(p1, p2);

                btr.AppendEntity(MyL1);

                trans.AddNewlyCreatedDBObject(MyL1, true);//L1

                Circle c1 = new Circle();

                c1.SetDatabaseDefaults();

                c1.Center = p2;

                c1.Radius = L2;

                Circle c2 = new Circle();

                c2.SetDatabaseDefaults();

                c2.Center = p4;

                c2.Radius = L3;

                Point3dCollection pointcoll = new Point3dCollection();

                c2.IntersectWith(c1, Intersect.OnBothOperands, pointcoll, 0, 0);

                p3 = pointcoll[1];

                Line MyL2 = new Line(p2, p3);

                Line MyL3 = new Line(p3, p4);

                btr.AppendEntity(MyL2);

                trans.AddNewlyCreatedDBObject(MyL2, true);//L2

                btr.AppendEntity(MyL3);

                trans.AddNewlyCreatedDBObject(MyL3, true);//L3

                trans.Commit();

            }

            ed.UpdateScreen();

            System.Threading.Thread.Sleep(50);

            theta += delt;

            i--;

            test();

        }

    }

} 

netload->dll文件->test

转完两圈就不转了,按下enter继续转动。

 有图有真相,下面是效果图:

折腾一下AutoCAD——四连杆的模拟运动 - Castor - 趁年轻,多折腾~~

 如果将语句:

  p3 = pointcoll[1];

改写为:

p3 = pointcoll[0];

则是四连杆的另外一种连接形式。

 

 

  评论这张
 
阅读(1244)| 评论(3)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2016