CAD用vba来输出图形数据
各位大侠:新人目前遇到个困难。想在cad加载宏,运行后把图形的直线和圆弧等图形数据通过txt文本文件输出来保存。Sub a()
Dim OBJ As AcadEntity '定义对象入口变量
Dim i As Integer '定义对象绘制顺序变量
i = 0 '初始化对象顺序变量
For Each OBJ In ThisDrawing.ModelSpace'遍历每个AutoCAD模型
If OBJ.ObjectName = "AcDbLine" Then '判断AutoCAD对象是不是直线
startp = OBJ.StartPoint '获取直线起点坐标
endp = OBJ.EndPoint '获取直线端点坐标
Print #1, "(" + CStr(i) + "," + CStr(startp(0)) + "," + CStr(startp(1)) + "," + CStr(endp(0)) + "," + CStr(endp(1)) + ");"
'输出直线端点坐标和该对象的绘制顺序
End If
If OBJ.ObjectName = "AcDbArc" Then '判断AutoCAD对象是不是圆弧
StartAngle = OBJ.StartAngle '获取圆弧起始角度
EndAngle = OBJ.EndAngle '获取圆弧终止角度
Radius = OBJ.Radius '获取圆弧半径
startp = OBJ.StartPoint '获取圆弧起点坐标
endp = OBJ.EndPoint '获取圆弧终点坐标
CenterPoint = OBJ.Center '获取圆弧中心坐标
Print #2, "(" + CStr(i) + "," + CStr(CenterPoint(0)) + "," + CStr(CenterPoint(1)) + "," + CStr(Radius) + "," + CStr(startp(0)) + "," + CStr(startp(1)) + "," + CStr(endp(0)) + "," + CStr(endp(1)) + "," + CStr(StartAngle) + "," + CStr(EndAngle - StartAngle) + ");"
'组合数据,输出圆弧的关键点数据
End If
i = i + 1 '对象绘制顺序序号加1
Dim a As String
a = "End If" '这里应该是输出上面要找到的数据,但我不知道怎么搞了
Open "d:\1.txt" For Append As #1
Print #1, a
Close #1
Next
End Sub
目前运行这个宏工程,能在d盘生成个1.txt的文件,但写入的是a = "End If" 里的End If字母,我想请大家给看看怎么修改后可以正常把我需要输出内容输出来。
http://bbs.mjtd.com/xwb/images/bgimg/icon_logo.png 该贴已经同步到 昊爹正传的微博 看来高手们也不愿意免费指导了,现在看只能出学费了,我只要这个功能:打开cad加载vba程序,把图中图形的直线或者圆弧或者多段线的数据输出保存为txt文本文件。我出200RMB.15979663114QQ759947720在线等 改成这样的:
Sub a()
Dim OBJ As AcadEntity '定义对象入口变量
Dim i As Integer '定义对象绘制顺序变量
i = 0 '初始化对象顺序变量
Open "d:\1.txt" For Append As #1
For Each OBJ In ThisDrawing.ModelSpace'遍历每个AutoCAD模型
If OBJ.ObjectName = "AcDbLine" Then '判断AutoCAD对象是不是直线
startp = OBJ.StartPoint '获取直线起点坐标
endp = OBJ.EndPoint '获取直线端点坐标
Print #1, "(" + CStr(i) + "," + CStr(startp(0)) + "," + CStr(startp(1)) + "," + CStr(endp(0)) + "," + CStr(endp(1)) + ");"
'输出直线端点坐标和该对象的绘制顺序
End If
If OBJ.ObjectName = "AcDbArc" Then '判断AutoCAD对象是不是圆弧
StartAngle = OBJ.StartAngle '获取圆弧起始角度
EndAngle = OBJ.EndAngle '获取圆弧终止角度
Radius = OBJ.Radius '获取圆弧半径
startp = OBJ.StartPoint '获取圆弧起点坐标
endp = OBJ.EndPoint '获取圆弧终点坐标
CenterPoint = OBJ.Center '获取圆弧中心坐标
Print #1, "(" + CStr(i) + "," + CStr(CenterPoint(0)) + "," + CStr(CenterPoint(1)) + "," + CStr(Radius) + "," + CStr(startp(0)) + "," + CStr(startp(1)) + "," + CStr(endp(0)) + "," + CStr(endp(1)) + "," + CStr(StartAngle) + "," + CStr(EndAngle - StartAngle) + ");"
'组合数据,输出圆弧的关键点数据
End If
i = i + 1 '对象绘制顺序序号加1
'Dim a As String
'a = "End If" '这里应该是输出上面要找到的数据,但我不知道怎么搞了
'Print #1, a
Next
Close #1
End Sub 小问题, 不用纠结了,For free
好东西!非常感谢分享 感谢“slyd “!热心助人是美德! 这个输出直线数据的代码,可不可以多输出一个角度啊,求指教
AcDbLine这个是什么意思,直线不是AcadLine吗 slyd 发表于 2014-5-11 17:09 static/image/common/back.gif
改成这样的:
Sub a()
您好,假设对象是文字,要把文字输出到#1里面应该怎么写呢?谢谢~初学者学习~~ 只列出了print输出的屏显的格式(好像书上说这个命令也能输出到文件,没具体研究过;一般输出到文本应该用write比较多吧),为什么不用write命令把赋了值的参数加表头一起格式化输出到文本文件呢?
没试过,凭自觉
页:
[1]