lwd899 发表于 2010-8-6 12:00:00

在UCS下画线出现问题

<p>如题,为什么我第二次画的线和第三次画的线重合?代码如下:</p>
<p><font style="BACKGROUND-COLOR: #ffffff" face="Verdana">'CAD<br/>Imports Autodesk.AutoCAD.Interop<br/>Imports Autodesk.AutoCAD.Interop.Common<br/>Public Class Form1<br/>&nbsp;&nbsp;&nbsp; Dim AcadApp As AcadApplication<br/>&nbsp;&nbsp;&nbsp; Dim thisdrawing As AcadDocument<br/>&nbsp;&nbsp;&nbsp; Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Try<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AcadApp = GetObject("autocad.application")<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Catch ex As Exception<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Try<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AcadApp = CreateObject("autocad.application")<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Catch ex1 As Exception<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MsgBox("无法打开AutoCAD")<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Exit Sub<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End Try<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End Try<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AcadApp.Visible = True<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AppActivate(AcadApp.Caption)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; thisdrawing = AcadApp.ActiveDocument<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; For i = 1 To 3<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim origin(0 To 2) As Double<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Call AddLine_UCS(1, 1, 0, 12, 12, 0)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'Dim origin(0 To 2) As Double<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; origin(0) = 8 : origin(1) = 8 : origin(2) = 0<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Call MoveOriginUCS(origin, "MyUcs")<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Next</font></p>
<p><font style="BACKGROUND-COLOR: #ffffff" face="Verdana">&nbsp;&nbsp;&nbsp; End Sub<br/>&nbsp;&nbsp;&nbsp; '通过移动坐标原点定义坐标系<br/>&nbsp;&nbsp;&nbsp; Public Function MoveOriginUCS(ByVal originWcs As Object, ByVal ucsName As String) As AcadUCS<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' 获得新UCS原点在当前UCS中的坐标<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim originUcs As Object<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; originUcs = TranslatePointWcsToUcs(originWcs)</font></p>
<p><font style="BACKGROUND-COLOR: #ffffff" face="Verdana">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' 获得X、Y正半轴上任一点的UCS坐标<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim ptXUcs(0 To 2) As Double, ptYUcs(0 To 2) As Double<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ptXUcs(0) = originUcs(0) + 1<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ptXUcs(1) = originUcs(1)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ptXUcs(2) = originUcs(2)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ptYUcs(0) = originUcs(0)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ptYUcs(1) = originUcs(1) + 1<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ptYUcs(2) = originUcs(2)</font></p>
<p><font style="BACKGROUND-COLOR: #ffffff" face="Verdana">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' 获得X、Y正半轴上任一点的WCS坐标<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim ptXWcs As Object, ptYWcs As Object<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; originWcs = TranslatePointUcsToWcs(originUcs)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ptXWcs = TranslatePointUcsToWcs(ptXUcs)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ptYWcs = TranslatePointUcsToWcs(ptYUcs)</font></p>
<p><font style="BACKGROUND-COLOR: #ffffff" face="Verdana">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' 创建UCS<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MoveOriginUCS = thisdrawing.UserCoordinateSystems.Add(originWcs, ptXWcs, ptYWcs, ucsName)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'MoveOriginUCS = thisdrawing.UserCoordinateSystems.Add(originUcs, ptXUcs, ptYUcs, ucsName)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; '' 显示 UCS 图标<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'thisdrawing.ActiveViewport.UCSIconAtOrigin = True<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'thisdrawing.ActiveViewport.UCSIconOn = True<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; '' 使新的 UCS 成为活动的 UCS<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'thisdrawing.ActiveUCS = MoveOriginUCS<br/>&nbsp;&nbsp;&nbsp; End Function<br/>&nbsp;&nbsp;&nbsp; ' 将点的坐标从UCS转换到WCS<br/>&nbsp;&nbsp;&nbsp; Public Function TranslatePointUcsToWcs(ByVal ucsPoint As Object) As Object<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Debug.Assert(VarType(ucsPoint) = vbArray + vbDouble)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Debug.Assert(LBound(ucsPoint) = 0 And UBound(ucsPoint) = 2)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TranslatePointUcsToWcs = thisdrawing.Utility.TranslateCoordinates(ucsPoint, AcCoordinateSystem.acUCS, AcCoordinateSystem.acWorld, False)<br/>&nbsp;&nbsp;&nbsp; End Function</font></p>
<p><font style="BACKGROUND-COLOR: #ffffff" face="Verdana">&nbsp;&nbsp;&nbsp; ' 将点的坐标从WCS转换到UCS<br/>&nbsp;&nbsp;&nbsp; Public Function TranslatePointWcsToUcs(ByVal wcsPoint As Object) As Object<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Debug.Assert(VarType(wcsPoint) = vbArray + vbDouble)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Debug.Assert(LBound(wcsPoint) = 0 And UBound(wcsPoint) = 2)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TranslatePointWcsToUcs = thisdrawing.Utility.TranslateCoordinates(wcsPoint, AcCoordinateSystem.acWorld, AcCoordinateSystem.acUCS, False)<br/>&nbsp;&nbsp;&nbsp; End Function<br/>&nbsp;&nbsp;&nbsp; Public Function AddLine_UCS(ByVal p1x As Double, ByVal p1y As Double, ByVal p1z As Double, ByVal p2x As Double, ByVal p2y As Double, ByVal p2z As Double) As AcadLine<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' 保存当前的UCS<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim curUcs As AcadUCS<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; curUcs = GetActiveUCS()<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' 返回到WCS<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; thisdrawing.ActiveUCS = GetWCS()<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim ptStart(2) As Double<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim ptEnd(2) As Double<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ptStart(0) = p1x<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ptStart(1) = p1y<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ptStart(2) = p1z</font></p>
<p><font style="BACKGROUND-COLOR: #ffffff" face="Verdana">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ptEnd(0) = p2x<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ptEnd(1) = p2y<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ptEnd(2) = p2z</font></p>
<p><font style="BACKGROUND-COLOR: #ffffff" face="Verdana">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' 在WCS中创建轻量多段线<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim objLine As AcadLine<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; objLine = thisdrawing.ModelSpace.AddLine(ptStart, ptEnd)</font></p>
<p><font style="BACKGROUND-COLOR: #ffffff" face="Verdana">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' 恢复保存的UCS<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; thisdrawing.ActiveUCS = curUcs</font></p>
<p><font style="BACKGROUND-COLOR: #ffffff" face="Verdana">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' 对长方体进行变换<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim transMatrix As Object<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; transMatrix = curUcs.GetUCSMatrix()<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; objLine.TransformBy(transMatrix)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; objLine.Update()</font></p>
<p><font style="BACKGROUND-COLOR: #ffffff" face="Verdana">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; AddLine_UCS = objLine<br/>&nbsp;&nbsp;&nbsp; End Function<br/>&nbsp;&nbsp;&nbsp; Public Function GetWCS() As AcadUCS<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' 定义创建UCS的三个点<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim ptOrigin(2) As Double, ptXAxis(2) As Double, ptYAxis(2) As Double<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ptOrigin(0) = 0 : ptOrigin(1) = 0 : ptOrigin(2) = 0<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ptXAxis(0) = 1 : ptXAxis(1) = 0 : ptXAxis(2) = 0<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ptYAxis(0) = 0 : ptYAxis(1) = 1 : ptYAxis(2) = 0<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GetWCS = thisdrawing.UserCoordinateSystems.Add(ptOrigin, ptXAxis, ptYAxis, "WCS")<br/>&nbsp;&nbsp;&nbsp; End Function<br/>&nbsp;&nbsp;&nbsp; Public Function GetActiveUCS() As AcadUCS<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If thisdrawing.GetVariable("UCSNAME") = "" Then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim ptOrigin(2) As Double&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' 要创建的UCS的原点<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim ptXAxis(2) As Double&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' UCS的X轴正半轴上一点<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim ptYAxis(2) As Double&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' UCS的Y轴正半轴上一点<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim xDir, yDir, org As Object&nbsp; ' 当前UCS的参数</font></p>
<p><font style="BACKGROUND-COLOR: #ffffff" face="Verdana">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' 获得当前UCS的参数<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; xDir = thisdrawing.GetVariable("UCSXDIR")<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; yDir = thisdrawing.GetVariable("UCSYDIR")<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; org = thisdrawing.GetVariable("UCSORG")</font></p>
<p><font style="BACKGROUND-COLOR: #ffffff" face="Verdana">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' UCS的原点<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ptOrigin(0) = org(0)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ptOrigin(1) = org(1)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ptOrigin(2) = org(2)</font></p>
<p><font style="BACKGROUND-COLOR: #ffffff" face="Verdana">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' 获得UCS的X轴正半轴上的一点<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ptXAxis(0) = org(0) + xDir(0)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ptXAxis(1) = org(1) + xDir(1)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ptXAxis(2) = org(2) + xDir(2)</font></p>
<p><font style="BACKGROUND-COLOR: #ffffff" face="Verdana">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' 获得UCS的Y轴正半轴上的一点<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ptYAxis(0) = org(0) + yDir(0)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ptYAxis(1) = org(1) + yDir(1)<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ptYAxis(2) = org(2) + yDir(2)</font></p>
<p><font style="BACKGROUND-COLOR: #ffffff" face="Verdana">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' 创建和当前UCS重合的UCS<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GetActiveUCS = thisdrawing.UserCoordinateSystems.Add(ptOrigin, ptXAxis, ptYAxis, "MyUCS")</font></p>
<p><font style="BACKGROUND-COLOR: #ffffff" face="Verdana">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; thisdrawing.ActiveUCS = GetActiveUCS<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Else<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GetActiveUCS = thisdrawing.ActiveUCS<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If<br/>&nbsp;&nbsp;&nbsp; End Function<br/>End Class</font></p>

lwd899 发表于 2010-8-10 10:53:00

问题已经解决了!

crazylsp 发表于 2013-5-22 17:28:04

楼上是怎么解决的啊?
页: [1]
查看完整版本: 在UCS下画线出现问题