明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2400|回复: 8

[几何] 求直线长度

[复制链接]
发表于 2011-1-14 11:12 | 显示全部楼层 |阅读模式
环境:AutoCAD2006+VS2008
要求:求0图层所有直线长度

代码如下:

    <CommandMethod("testFilSel")> Public Sub testSelection2()
        Dim db As Database = HostApplicationServices.WorkingDatabase
        Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
        '定义过滤条件
        '实体类型
        Dim value1 As TypedValue = New TypedValue(DxfCode.Start, "LINE")
        '图层
        Dim value2 As TypedValue = New TypedValue(DxfCode.LayerName, "0")
        'Dim value3 As TypedValue = New TypedValue(DxfCode.Color, "1")
        Dim values() As TypedValue = {value1, value2}
        Dim sfilter As New SelectionFilter(values)

        ' 返回选择集的用户提示类.
        Dim resSel As PromptSelectionResult = ed.SelectAll(sfilter)
        ' 得到选择集对象.
        Dim sSet As SelectionSet = resSel.Value
        ' 得到选择集中所有对象的ObjectId集合.
        Dim ids As ObjectId() = sSet.GetObjectIds()
        Using trans As Transaction = db.TransactionManager.StartTransaction()
            ' 遍历选择集.
            For Each sSetEntId As ObjectId In ids
                Dim ent As Entity = trans.GetObject(sSetEntId, OpenMode.ForWrite)
            
                ' 这里怎么写
                'Dim l As Double

            Next
            trans.Commit()
        End Using
    End Sub
发表于 2011-1-14 16:36 | 显示全部楼层
求曲线长度的通用代码:
ent.GetDistanceAtParameter(ent.EndParam)
回复 支持 1 反对 0

使用道具 举报

发表于 2011-1-14 12:17 | 显示全部楼层
  1.             Dim sum As Double
  2.             For Each sSetEntId As ObjectId In ids
  3.                 Dim ent As Line = trans.GetObject(sSetEntId, OpenMode.ForWrite)
  4.                 sum += ent.Length
  5.             Next
发表于 2011-1-14 12:30 | 显示全部楼层
trans.GetObject(sSetEntId, OpenMode.ForRead)即可……
发表于 2011-1-14 12:32 | 显示全部楼层
随手复制了,呵呵
另外,如果没有对数据库有更改,只是查询的话
trans.Commit()也可以去掉
 楼主| 发表于 2011-1-14 14:04 | 显示全部楼层
本帖最后由 hnzgs 于 2011-1-14 14:07 编辑

感谢楼上两位。

是不是版本不一样,我用的CAD2006+vs2008,
调试提示:
“Length”不是“Autodesk.AutoCAD.DatabaseServices.Line”的成员

这样实现不知道是否有误差
Dim vec As Vector3d = ent.GeomExtents.MaxPoint - ent.GeomExtents.MinPoint
Length = Length + vec.Length

 楼主| 发表于 2011-1-14 16:58 | 显示全部楼层
多谢飞狐版主~~~
发表于 2012-6-26 17:45 | 显示全部楼层
参考着学习了~~呵呵~
发表于 2012-7-31 14:01 | 显示全部楼层
新手上路,看到楼上那些交流觉得C#好难学啊[em0]
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-4-19 19:08 , Processed in 0.244616 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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