120010139 发表于 2005-5-12 22:56:00

请各位大虾帮忙看看这个程序哪里错在哪里 急~~~

小弟初学VBA不久,最近做了一个练习.这个程序的目的可以实现CAD图中实体变成三视图



但小弟在运行过程中老是出错,请各位大虾帮帮忙!!谢谢了!!!!


Sub 三视图显示()


'俯视显示视图<BR>ThisDrawing.SendCommand ("_-view" + vbCr + "_top" + vbCr)


'找出图中齿轮实体<BR>Dim returnObj As Object<BR>Dim Obj3D As Boolean


For Each returnObj In ThisDrawing.ModelSpace<BR>                       If returnObj.ObjectName = "AcDb3dsolid" Then<BR>                       Obj3D = True<BR>                       Exit For<BR>                       End If<BR>Next


'若图中无齿轮实体,退出过程<BR>If Obj3D = False Then<BR>               MsgBox "图中无齿轮实体", vbOKOnly, "重要提示!"<BR>               Exit Sub<BR>End If


'获取包含实体外框的最大、最小点坐标<BR>Dim minExt As Variant<BR>Dim maxExt As Variant


returnObj.GetBoundingBox minExt, maxExt


'将齿轮实体复制3份<BR>Dim returnObjLeft As Object '左视图<BR>Dim returnObjTop As Object '俯视图<BR>Dim returnObjSouthWest As Object '轴测视图


Set returnObjLeft = returnObj.Copy<BR>Set returnObjTop = returnObj.Copy<BR>Set returnObjSouthWest = returnObj.Copy


'生成左视图<BR>Dim pt1(2), pt2(2) As Double<BR>pt1(0) = (minExt(0) + maxExt(0)) / 2: pt1(1) = (minExt(1) + maxExt(1)) / 2: pt1(2) = _<BR>(minExt(2) + maxExt(2)) / 2<BR>pt2(0) = pt1(0): pt2(1) = pt1(1) + 1: pt2(2) = pt1(2)


'旋转90度得左视图<BR>returnObjLeft.Rotate3D pt1, pt2, 3.1415 / 2


Dim ScaleD As Double '放大视图间的距离<BR>ScaleD = 1.2


pt1(0) = (minExt(0) + maxExt(0)) / 2: pt1(1) = (minExt(1) + maxExt(1)) / 2: pt1(2) = _<BR>(minExt(2) + maxExt(2)) / 2<BR>pt2(0) = pt1(0) + (maxExt(0) - minExt(0)) * ScaleD: pt2(1) = pt1(1): pt2(2) = pt1(2)


'向右移动左视图<BR>returnObjLeft.Move pt1, pt2


'生成俯视图<BR>pt1(0) = (minExt(0) + maxExt(0)) / 2: pt1(1) = (minExt(1) + maxExt(1)) / 2: pt1(2) = _<BR>(minExt(2) + maxExt(2)) / 2<BR>pt2(0) = pt1(0) + 1: pt2(1) = pt1(1): pt2(2) = pt1(2)


'旋转90度得俯视图<BR>returnObjTop.Rotate3D pt1, pt2, 3.1415 / 2


pt1(0) = (minExt(0) + maxExt(0)) / 2: pt1(1) = (minExt(1) + maxExt(1)) / 2: pt1(2) = _<BR>(minExt(2) + maxExt(2)) / 2<BR>pt2(0) = pt1(0): pt2(1) = pt1(1) - (maxExt(0) - minExt(0)) * ScaleD: pt2(2) = pt1(2)


'向下移动俯视图<BR>returnObjTop.Move pt1, pt2


'生成轴测视图<BR>pt1(0) = (minExt(0) + maxExt(0)) / 2: pt1(1) = (minExt(1) + maxExt(1)) / 2: pt1(2) = _<BR>(minExt(2) + maxExt(2)) / 2<BR>pt2(0) = pt1(0) + 1: pt2(1) = pt1(1): pt2(2) = pt1(2)


'旋转45度<BR>returnObjSouthWest.Rotate3D pt1, pt2, -3.1415 / 4


pt1(0) = (minExt(0) + maxExt(0)) / 2: pt1(1) = (minExt(1) + maxExt(1)) / 2: pt1(2) = _<BR>(minExt(2) + maxExt(2)) / 2<BR>pt2(0) = pt1(0): pt2(1) = pt1(1) + 1: pt2(2) = pt1(2)


'旋转45度<BR>returnObjSouthWest.Rotate3D pt1, pt2, -3.1415 / 4


pt1(0) = (minExt(0) + maxExt(0)) / 2: pt1(1) = (minExt(1) + maxExt(1)) / 2: pt1(2) = _<BR>(minExt(2) + maxExt(2)) / 2<BR>pt2(0) = pt1(0) + (maxExt(0) - minExt(0)) * ScaleD: pt2(1) = pt1(1) - (maxExt(1) - _<BR>minExt(1)) * ScaleD: pt2(2) = pt1(2)


'向右下移动轴测视图<BR>returnObjSouthWest.Move pt1, pt2


'三点定义左视图的剖切平面<BR>Dim slicePt1(2) As Double<BR>Dim slicePt2(2) As Double<BR>Dim slicePt3(2) As Double


slicePt1(0) = (minExt(0) + maxExt(0)) / 2: slicePt1(1) = (minExt(1) + maxExt(1)) / 2: _<BR>slicePt1(2) = (minExt(2) + maxExt(2)) / 2<BR>slicePt2(0) = slicePt1(0): slicePt2(1) = slicePt1(1) + 1: slicePt2(2) = slicePt1(2)<BR>slicePt3(0) = slicePt1(0) + 1: slicePt3(1) = slicePt1(1): slicePt3(2) = slicePt1(2)


'剖切左视图<BR>Dim sliceObj As AutoCAD.Acad3DSolid<BR>Set sliceObj = returnObjLeft.SliceSolid(slicePt1, slicePt2, slicePt3, True)<BR>returnObjLeft.Delete '删除returnObjLeft,余下剖切图


ThisDrawing.SendCommand ("_Shademode" + vbCr + "_G" + vbCr) '着色


ThisDrawing.Application.ZoomExtents<BR>End Sub

120010139 发表于 2005-5-13 11:44:00

没有大虾帮帮我这个小虾米啊..

今晚打老虎 发表于 2005-5-13 17:58:00

Sub 三视图显示()


'俯视显示视图<BR>ThisDrawing.SendCommand ("_-view" + vbCr + "_top" + vbCr)


'找出图中齿轮实体<BR>Dim returnObj As Object<BR>Dim Obj3D As Boolean


For Each returnObj In ThisDrawing.ModelSpace<BR>                       If TypeName(returnObj) = "IAcad3DSolid" Then<BR>                       Obj3D = True<BR>                       Exit For<BR>                       End If<BR>Next


'若图中无齿轮实体,退出过程<BR>If Obj3D = False Then<BR>               MsgBox "图中无齿轮实体", vbOKOnly, "重要提示!"<BR>               Exit Sub<BR>End If


'获取包含实体外框的最大、最小点坐标<BR>Dim minExt As Variant<BR>Dim maxExt As Variant


returnObj.GetBoundingBox minExt, maxExt


'将齿轮实体复制3份<BR>Dim returnObjLeft As AcadEntity       '左视图<BR>Dim returnObjTop As AcadEntity '俯视图<BR>Dim returnObjSouthWest As AcadEntity '轴测视图


Set returnObjLeft = returnObj.Copy<BR>Set returnObjTop = returnObj.Copy<BR>Set returnObjSouthWest = returnObj.Copy


'生成左视图<BR>Dim pt1(2) As Double, pt2(2) As Double<BR>pt1(0) = (minExt(0) + maxExt(0)) / 2: pt1(1) = (minExt(1) + maxExt(1)) / 2: pt1(2) = _<BR>(minExt(2) + maxExt(2)) / 2<BR>pt2(0) = pt1(0): pt2(1) = -pt1(1): pt2(2) = pt1(2)


'旋转90度得左视图<BR>returnObjLeft.Rotate3D pt1, pt2, 3.1415 / 2


Dim ScaleD As Double '放大视图间的距离<BR>ScaleD = 1.2


pt1(0) = (minExt(0) + maxExt(0)) / 2: pt1(1) = (minExt(1) + maxExt(1)) / 2: pt1(2) = _<BR>(minExt(2) + maxExt(2)) / 2<BR>pt2(0) = pt1(0) + (maxExt(0) - minExt(0)) * ScaleD: pt2(1) = pt1(1): pt2(2) = pt1(2)


'向右移动左视图<BR>returnObjLeft.Move pt1, pt2


'生成俯视图<BR>pt1(0) = (minExt(0) + maxExt(0)) / 2: pt1(1) = (minExt(1) + maxExt(1)) / 2: pt1(2) = _<BR>(minExt(2) + maxExt(2)) / 2<BR>pt2(0) = pt1(0) + 1: pt2(1) = pt1(1): pt2(2) = pt1(2)


'旋转90度得俯视图<BR>returnObjTop.Rotate3D pt1, pt2, 3.1415 / 2


pt1(0) = (minExt(0) + maxExt(0)) / 2: pt1(1) = (minExt(1) + maxExt(1)) / 2: pt1(2) = _<BR>(minExt(2) + maxExt(2)) / 2<BR>pt2(0) = pt1(0): pt2(1) = pt1(1) - (maxExt(0) - minExt(0)) * ScaleD: pt2(2) = pt1(2)


'向下移动俯视图<BR>returnObjTop.Move pt1, pt2


'生成轴测视图<BR>pt1(0) = (minExt(0) + maxExt(0)) / 2: pt1(1) = (minExt(1) + maxExt(1)) / 2: pt1(2) = _<BR>(minExt(2) + maxExt(2)) / 2<BR>pt2(0) = pt1(0) + 1: pt2(1) = pt1(1): pt2(2) = pt1(2)


'旋转45度<BR>returnObjSouthWest.Rotate3D pt1, pt2, -3.1415 / 4


pt1(0) = (minExt(0) + maxExt(0)) / 2: pt1(1) = (minExt(1) + maxExt(1)) / 2: pt1(2) = _<BR>(minExt(2) + maxExt(2)) / 2<BR>pt2(0) = pt1(0): pt2(1) = pt1(1) + 1: pt2(2) = pt1(2)


'旋转45度<BR>returnObjSouthWest.Rotate3D pt1, pt2, -3.1415 / 4


pt1(0) = (minExt(0) + maxExt(0)) / 2: pt1(1) = (minExt(1) + maxExt(1)) / 2: pt1(2) = _<BR>(minExt(2) + maxExt(2)) / 2<BR>pt2(0) = pt1(0) + (maxExt(0) - minExt(0)) * ScaleD: pt2(1) = pt1(1) - (maxExt(1) - _<BR>minExt(1)) * ScaleD: pt2(2) = pt1(2)


'向右下移动轴测视图<BR>returnObjSouthWest.Move pt1, pt2


'三点定义左视图的剖切平面<BR>Dim slicePt1(2) As Double<BR>Dim slicePt2(2) As Double<BR>Dim slicePt3(2) As Double


slicePt1(0) = (minExt(0) + maxExt(0)) / 2: slicePt1(1) = (minExt(1) + maxExt(1)) / 2: _<BR>slicePt1(2) = (minExt(2) + maxExt(2)) / 2<BR>slicePt2(0) = slicePt1(0): slicePt2(1) = slicePt1(1) + 1: slicePt2(2) = slicePt1(2)<BR>slicePt3(0) = slicePt1(0) + 1: slicePt3(1) = slicePt1(1): slicePt3(2) = slicePt1(2)


'剖切左视图<BR>Dim sliceObj As AutoCAD.Acad3DSolid<BR>Set sliceObj = returnObjLeft.SliceSolid(slicePt1, slicePt2, slicePt3, True)<BR>returnObjLeft.Delete '删除returnObjLeft,余下剖切图


ThisDrawing.SendCommand ("_Shademode" + vbCr + "_G" + vbCr) '着色


ThisDrawing.Application.ZoomExtents<BR>End Sub


<BR>

120010139 发表于 2005-5-14 10:23:00

谢谢楼上的大大!!!感激之情如黄河水滔滔不绝啊:)
页: [1]
查看完整版本: 请各位大虾帮忙看看这个程序哪里错在哪里 急~~~