明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 25880|回复: 31

[基础] TlsBasal(基础类库,开源部分代码)

    [复制链接]
发表于 2009-5-17 23:45 | 显示全部楼层 |阅读模式
本帖最后由 lzh741206 于 2010-12-22 13:15 编辑

(2012/12/22)
](2012/12/12)
2010/12/12
增强DBTransaction类
添加TlsBasal.Xml程序注释文件
2010/10/29
少许更新,加强扩展函数调用
2010/4/24
VS2008/AutoCAD2008以上
加入扩展方法支持
集成ResultList/ResultTree集合类
集成可序列化集合类SerialList
2009/6/20
修正曲线转换类


  1.         [CommandMethod("Tls:BAC")]
  2.         public static void BreakAllCurve()
  3.         {
  4.             using (DBTransaction t = new DBTransaction())
  5.             {
  6.                 PromptSelectionResult res = Helper.Editor.GetSelection(
  7.                     new PromptSelectionOptions(),
  8.                     new SelectionFilter(
  9.                         new TypedValue[] {
  10.                             new TypedValue(0, "*Line,Arc,Circle,Ellipse") }));
  11.                 List<ObjectId> ids = new List<ObjectId>(res.Value.GetObjectIds());
  12.                 List<Entity> objs = BreakCurve(ref ids);
  13.                 t.OpenCurrentSpace();
  14.                 t.AddEntity(objs);
  15.                 t.Remove(ids);
  16.             }
  17.         }

  18.         public static List<Entity> BreakCurve(ref List<ObjectId> ids)
  19.         {
  20.             List<Curve> dbCurves = new List<Curve>();
  21.             List<CompositeCurve3d> geCurves = new List<CompositeCurve3d>();
  22.             List<List<double>> paramss = new List<List<double>>();
  23.             for (int i = 0; i < ids.Count; i++)
  24.             {
  25.                 dbCurves.Add((Curve)ids[i].GetObject(OpenMode.ForRead));
  26.                 geCurves.Add(ConvertCurve.ToCompositeCurve3d(dbCurves[i]));
  27.                 paramss.Add(new List<double>());
  28.             }

  29.             List<ObjectId> oldids = new List<ObjectId>();
  30.             List<Entity> objs = new List<Entity>();
  31.             CurveCurveIntersector3d cci3d = new CurveCurveIntersector3d();
  32.             for (int i = 0; i < ids.Count; i++)
  33.             {
  34.                 CompositeCurve3d gc1 = geCurves[i];
  35.                 List<double> pars1 = paramss[i];
  36.                 for (int j = i; j < ids.Count; j++)
  37.                 {
  38.                     CompositeCurve3d gc2 = geCurves[j];
  39.                     List<double> pars2 = paramss[j];
  40.                     cci3d.Set(gc1, gc2, Vector3d.ZAxis);
  41.                     for (int k = 0; k < cci3d.NumberOfIntersectionPoints; k++)
  42.                     {
  43.                         double[] pars = cci3d.GetIntersectionParameters(k);
  44.                         pars1.Add(pars[0]);
  45.                         pars2.Add(pars[1]);
  46.                     }
  47.                 }
  48.                 if (pars1.Count > 0)
  49.                 {
  50.                     List<CompositeCurve3d> c3ds = GeUtility.GetSplitCurves(gc1, pars1);
  51.                     if (c3ds.Count > 1)
  52.                     {
  53.                         foreach (CompositeCurve3d c3d in c3ds)
  54.                         {
  55.                             Curve c = ConvertCurve.ToCurve(c3d);
  56.                             c.SetPropertiesFrom(dbCurves[i]);
  57.                             objs.Add(c);
  58.                         }
  59.                         oldids.Add(ids[i]);
  60.                     }
  61.                 }
  62.             }
  63.             ids = oldids;
  64.             return objs;
  65.         }
2009/6/18
更新版本
增加ConvertCurve类
支持DbCurve 与 GeCurve 互转
但二维样条化多段线的转换不太成功
支持AutoCad2008版本以上,低版本未测试
1、DBTransaction类
简化AutoCad托管程序写法的自定义类,封装了一些常用的函数,完善中
2009/5/27完善Ucs下属性块插入

用法的示例
  1.         [CommandMethod("BAC")]
  2.         public static void BreakAllCurve()
  3.         {
  4.             PromptSelectionResult res =
  5.                 CadHelper.Editor.GetSelection(
  6.                 new PromptSelectionOptions(),
  7.                 new SelectionFilter(new TypedValue[] {
  8.                     new TypedValue(0, "*Line,Arc,Circle,Ellipse") }));
  9.             ObjectId[] ids = res.Value.GetObjectIds();
  10.             ObjectIdCollection oldids = new ObjectIdCollection();
  11.             using (DBTransaction t = new DBTransaction())
  12.             {
  13.                 t.OpenCurrentSpace();
  14.                 foreach (ObjectId i in ids)
  15.                 {
  16.                     List<double> pars = new List<double>();
  17.                     Curve iCurve = (Curve)t.GetObject(i, OpenMode.ForRead);
  18.                     foreach (ObjectId j in ids)
  19.                     {
  20.                         if (i != j)
  21.                         {
  22.                             Curve jCurve = (Curve)t.GetObject(j, OpenMode.ForRead);
  23.                             Point3dCollection iwpnts = new Point3dCollection();
  24.                             iCurve.IntersectWith(jCurve, Intersect.OnBothOperands, iwpnts, 0, 0);
  25.                             foreach (Point3d p in iwpnts)
  26.                             {
  27.                                 pars.Add(iCurve.GetParameterAtPoint(p));
  28.                             }
  29.                         }
  30.                     }
  31.                     if (pars.Count > 0)
  32.                     {
  33.                         pars.Sort();
  34.                         try
  35.                         {
  36.                             t.AddEntity(iCurve.GetSplitCurves(new DoubleCollection(pars.ToArray())));
  37.                             oldids.Add(i);
  38.                         }
  39.                         catch
  40.                         { }
  41.                     }
  42.                 }
  43.                 t.Remove(oldids);
  44.             }
  45.         }

2、BlockPreview类
预览图像(块定义/图元集合)

将下面的文件Copy到d盘根目录下

3、TlsCad.Preferences名称空间
提供反射获取Cad环境变量的一些静态类,
当然你也可以使用Com引用获取Cad环境变量
调用方式
  1. //获取autocad支持目录
  2. string s = TlsCad.Preferences.Files.SupportPath;
  3. //设置autocad支持目录
  4. TlsCad.Preferences.Files.SupportPath = s + ";c:\";
  5. //or
  6. TlsCad.Preferences.Files.SupportPath += ";d:\";
复制代码

4、TlsCad.Utils名称空间
提供一些常用函数调用

本帖子中包含更多资源

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

x

评分

参与人数 1威望 +2 明经币 +5 金钱 +20 贡献 +5 激情 +5 收起 理由
ahlzl + 2 + 5 + 20 + 5 + 5 【精华】好文章

查看全部评分

本帖被以下淘专辑推荐:

  • · c#|主题: 6, 订阅: 2
发表于 2016-9-26 13:57 | 显示全部楼层
这个功能有点用,可以借用一下
发表于 2020-4-2 11:56 | 显示全部楼层
学习了  码上
 楼主| 发表于 2009-5-27 17:15 | 显示全部楼层
本帖最后由 作者 于 2009-5-27 23:23:39 编辑

0.2测试版
2009/5/27
更正ucs下属性块的插入
下面是一段简单的测试代码
  1.         [CommandMethod("tt2")]
  2.         public static void Test10()
  3.         {
  4.             using (DBTransaction tr = new DBTransaction())
  5.             {
  6.                 ObjectId blkdefid = tr.AddBlock("*T");
  7.                 tr.OpenBlockTableRecord(blkdefid);
  8.                 tr.AddEntity(new Line(Point3d.Origin, new Point3d(10, 0, 0)));
  9.                 tr.OpenTextStyleTable();
  10.                 AttributeDefinition attdef =
  11.                     new AttributeDefinition(
  12.                         Point3d.Origin,
  13.                         "Test",
  14.                         "A1",
  15.                         "Input A1",
  16.                         tr.TextStyleTable["Standard"]);
  17.                 attdef.Height = 5;
  18.                 tr.AddEntity(attdef);
  19.                 tr.OpenCurrentSpace();
  20.                 if (blkdefid != ObjectId.Null)
  21.                 {
  22.                     List<string> atts = new List<string>();
  23.                     atts.Add("This is a Test");
  24.                     tr.InsertBlock(blkdefid, atts);
  25.                 }
  26.             }
  27.         }

 楼主| 发表于 2009-5-30 10:32 | 显示全部楼层
本帖最后由 作者 于 2009-8-30 9:59:51 编辑

TlsCad开源部分的代码:
曲线转换类
http://bbs.mjtd.com/forum.php?mod=viewthread&tid=77565
DBTransaction类
http://bbs.mjtd.com/forum.php?mod=viewthread&tid=76123
曲线专贴
http://bbs.mjtd.com/forum.php?mod=viewthread&tid=75636
2009/5/30
增强动画录制功能,可以将动作保存为动态Gif文件或多帧Tiff
点击 开始 录制
点击 停止 将在C盘下保存两个文件

将BlockView的窗体代码改下:)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Windows.Forms;
  6. using System.Drawing.Imaging;
  7. using Autodesk.AutoCAD.DatabaseServices;
  8. using Autodesk.AutoCAD.Geometry;
  9. using Autodesk.AutoCAD.GraphicsInterface;
  10. using Autodesk.AutoCAD.Runtime;
  11. using Autodesk.AutoCAD.ApplicationServices;
  12. using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
  13. using TlsCad.Image;
  14. [assembly: CommandClass(typeof(TlsCad.frmBlockPreview))]
  15. namespace TlsCad
  16. {
  17.     public partial class frmBlockPreview : Form
  18.     {
  19.         BlockPreviewCollection m_BlockPreviews;
  20.         private ImageCreator m_Gif;
  21.         private ImageCreator m_Tiff;
  22.         public frmBlockPreview()
  23.         {
  24.             InitializeComponent();
  25.             m_BlockPreviews =
  26.                 new BlockPreviewCollection(
  27.                     "D:\\TlsCad.dwg",
  28.                     Autodesk.AutoCAD.GraphicsSystem.RenderMode.GouraudShaded,
  29.                     VisualStyleType.Gouraud);
  30.             m_BlockPreviews.BackColor = Color.White;
  31.             List<Entity> ents = new List<Entity>();
  32.             Line line = new Line(Point3d.Origin, new Point3d(0, 10.5, 0));
  33.             line.SetDatabaseDefaults();
  34.             ents.Add(line);
  35.             Circle cir = new Circle(Point3d.Origin, Vector3d.ZAxis, 14);
  36.             cir.SetDatabaseDefaults();
  37.             ents.Add(cir);
  38.             Line line1 = new Line(new Point3d(11, 0, 0), new Point3d(14, 0, 0));
  39.             Line line2 = new Line(new Point3d(13, 0, 0), new Point3d(14, 0, 0));
  40.             line1.SetDatabaseDefaults();
  41.             line2.SetDatabaseDefaults();
  42.             double angle = Math.PI / 30;
  43.             for (int i = 0; i < 60; i++)
  44.             {
  45.                 Line l;
  46.                 if (i % 5 == 0)
  47.                     l = (Line)line1.Clone();
  48.                 else
  49.                     l = (Line)line2.Clone();
  50.                 l.TransformBy(Matrix3d.Rotation(angle * i, Vector3d.ZAxis, Point3d.Origin));
  51.                 ents.Add(l);
  52.             }
  53.             m_BlockPreviews.Add(
  54.                 this.panel1,
  55.                 ents,
  56.                 new Extents3d(
  57.                     new Point3d(-20, -20, 0),
  58.                     new Point3d(20, 20, 0)));
  59.             BlockPreview bpv = m_BlockPreviews.Add(panel2, "PressureVessel");
  60.         }
  61.         private void frmBlockPreview_FormClosed(object sender, FormClosedEventArgs e)
  62.         {
  63.             m_BlockPreviews.Dispose();
  64.         }
  65.         private void button1_Click(object sender, EventArgs e)
  66.         {
  67.             button1.Text = (timer1.Enabled = !timer1.Enabled) ? "停止" : "继续";
  68.             if (timer1.Enabled)
  69.             {
  70.                 BlockPreview bpv = m_BlockPreviews[0];
  71.                 m_Gif = new GifCreator("c:\\1.gif", 100, 0);
  72.                 m_Tiff = new TiffCreator("c:\\1.tiff");
  73.                 Bitmap bp = m_BlockPreviews[0].GetImage();
  74.                 m_Gif.AddFrame(bp, FrameStatus.First);
  75.                 m_Tiff.AddFrame(bp, FrameStatus.First);
  76.             }
  77.             else
  78.             {
  79.                 Bitmap bp = m_BlockPreviews[0].GetImage();
  80.                 m_Gif.AddFrame(bp, FrameStatus.End);
  81.                 m_Tiff.AddFrame(bp, FrameStatus.End);
  82.             }
  83.         }
  84.         private void button2_Click(object sender, EventArgs e)
  85.         {
  86.             this.Close();
  87.         }
  88.         private void button3_Click(object sender, EventArgs e)
  89.         {
  90.             m_BlockPreviews[0].Save("c://1.jpg", ImageFormat.Jpeg);
  91.             m_BlockPreviews[1].Save("c://2.png", ImageFormat.Png);
  92.             MessageBox.Show("文件已保存!");
  93.         }  
  94.         
  95.         private void timer1_Tick(object sender, EventArgs e)
  96.         {
  97.             BlockPreview bpv = m_BlockPreviews[0];
  98.             Entity ent = bpv[0];
  99.             ent.TransformBy(Matrix3d.Rotation(-Math.PI / 300, Vector3d.ZAxis, Point3d.Origin));
  100.             bpv.Update(0);
  101.             bpv.Refresh();
  102.             Bitmap bp = m_BlockPreviews[0].GetImage();
  103.             m_Gif.AddFrame(bp, FrameStatus.Other);
  104.             m_Tiff.AddFrame(bp, FrameStatus.Other);
  105.         }
  106.         [CommandMethod("bview")]
  107.         static public void bview()
  108.         {
  109.             using (frmBlockPreview dlg = new frmBlockPreview())
  110.             {
  111.                 AcadApp.ShowModalDialog(dlg);
  112.             }
  113.         }
  114.     }
  115. }
BlockView挺有意思的:),简单的做了个方块测试下

把测试代码还是放上吧,同时Debug目录下有TlsBasal.dll的新版本
这让我想起了自己编的第一个程序就是方块,哈哈
方块测试结束

本帖子中包含更多资源

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

x
 楼主| 发表于 2009-6-3 00:26 | 显示全部楼层
本帖最后由 作者 于 2009-6-3 18:49:51 编辑

BlockView挺有意思的:),简单的做了个方块测试下

把测试代码还是放上吧,同时Debug目录下有TlsBasal.dll的新版本

这让我想起了自己编的第一个程序就是方块,哈哈

方块测试结束

本帖子中包含更多资源

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

x
 楼主| 发表于 2009-6-4 21:54 | 显示全部楼层

测试过程发现的一个问题

无标题栏的窗体调整位置真麻烦,干脆用代码把它固定住

            Window w = ACadApp.DocumentManager.MdiActiveDocument.Window;
            Point pnt = w.Location;
            Size size = w.Size;
            pnt.X += (size.Width - 505);
            pnt.Y += (size.Height - 622);
            string bounds = pnt.X + "," + pnt.Y + ",502,602";
            UserConfigurationManager ucm = ACadApp.UserConfigurationManager;
            IConfigurationSection ds = ucm.OpenDialogSection(this);
            ds.WriteProperty("Bounds", bounds);

不知道有没有更好的办法?

 楼主| 发表于 2009-6-18 11:48 | 显示全部楼层

2009/6/18

更新版本

发表于 2009-7-16 18:09 | 显示全部楼层
太有才了.
发表于 2009-7-18 18:23 | 显示全部楼层

老大.你文件没有发完啊.我搞二十分钟.都不知怎么开始.是不是自己再组合代码?

 楼主| 发表于 2009-7-18 18:34 | 显示全部楼层

二楼的代码是可以直接用的

一楼是新一点的版本,有一些改动

发表于 2009-7-18 18:35 | 显示全部楼层
还是没有整成功
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-5-4 17:08 , Processed in 0.411443 second(s), 28 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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