将项目添加到区域-错误?
:?很好的一天用一些代码创建一个包含各种项目的区域:
代码在红色高位线上出现异常:
Using acTrans As Autodesk.AutoCAD.DatabaseServices.Transaction = acCurDb.TransactionManager.StartTransaction()
Dim acBlkTbl As Autodesk.AutoCAD.DatabaseServices.BlockTable
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)
Dim acBlkTblRec As Autodesk.AutoCAD.DatabaseServices.BlockTableRecord
acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
Dim acDBObjCol As DBObjectCollection = New DBObjectCollection()
acDBObjCol.Add(Line1)
acDBObjCol.Add(Line2)
acDBObjCol.Add(Line3)
acDBObjCol.Add(Line4)
acDBObjCol.Add(Line5)
acDBObjCol.Add(Line6)
MsgBox(Line1.EndPoint.X)
Dim myRegionCol As DBObjectCollection = New DBObjectCollection()
myRegionCol = Autodesk.AutoCAD.DatabaseServices.Region.CreateFromCurves(acDBObjCol)
Dim acRegion As Autodesk.AutoCAD.DatabaseServices.Region = myRegionCol(0)
acBlkTblRec.AppendEntity(acRegion)
acTrans.AddNewlyCreatedDBObject(acRegion, True)
End Using
Catch ex As Exception
MsgBox("Error")
End Try
线条是用这个公共功能处理的:“Friend Function DrawLine”
返回该行。
生成的行:“
Line1=DrawLine(PntSt,ModDrawLine.DTR(AngLine),LineLng)”
Friend Function DrawLine(ByVal startPoint As Point3d, ByVal angle As Double, ByVal length As Double) As Line
Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
Dim acCurDb As Database = acDoc.Database
acDoc.LockDocument()
Dim id As ObjectId
Dim line As Line = Nothing
Using db As Database = HostApplicationServices.WorkingDatabase()
Dim endpoint As Point3d = ModDrawLine.PolarPoint(startPoint, angle, length)
startPoint = ModDrawLine.TransformByUCS(startPoint, db)
endpoint = ModDrawLine.TransformByUCS(endpoint, db)
Using tr As Transaction = db.TransactionManager.StartTransaction
Try
Dim btr As BlockTableRecord = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite, False)
line = New Line(startPoint, endpoint)
id = btr.AppendEntity(line)
db.TransactionManager.AddNewlyCreatedDBObject(line, True)
tr.Commit()
Catch ex As Exception
tr.Abort()
End Try
End Using
End Using
Return line
End Function
欢迎任何帮助
当做
穴居人密码 我很确定你的行没有创建封闭区域,因为这是工作代码
为了确保更好地使用闭合多段线或绘制直线链测长度
取决于以前的线点,例如:
' by Tony Tanzillo
Friend Function PolarPoint(basepoint As Point3d, angle As Double, distance As Double) As Point3d
Return New Point3d(basepoint.X + (distance * Math.Cos(angle)), basepoint.Y + (distance * Math.Sin(angle)), basepoint.Z)
End Function
<CommandMethod("demoreg")> _
Public Sub RegionDemo()
Dim acCurDb As Database = HostApplicationServices.WorkingDatabase
Dim ed As Editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor
Try
Using acTrans As Autodesk.AutoCAD.DatabaseServices.Transaction = acCurDb.TransactionManager.StartTransaction()
Dim acBlkTbl As Autodesk.AutoCAD.DatabaseServices.BlockTable
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)
Dim acBlkTblRec As Autodesk.AutoCAD.DatabaseServices.BlockTableRecord
acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
Dim acDBObjCol As DBObjectCollection = New DBObjectCollection()
Dim Line1 As Line = DrawLine(New Point3d(0, 0, 0), 0.0, 100)
Dim Line2 As Line = DrawLine(Line1.EndPoint, Math.PI / 2 - Math.PI / 6, 100)
Dim Line3 As Line = DrawLine(Line2.EndPoint, Math.PI / 2 + Math.PI / 6, 100)
Dim Line4 As Line = DrawLine(Line3.EndPoint, Math.PI, 100)
Dim Line5 As Line = DrawLine(Line4.EndPoint, Math.PI * 1.5 - Math.PI / 6, 100)
Dim Line6 As Line = DrawLine(Line5.EndPoint, Math.PI * 1.5 + Math.PI / 6, 100)
acDBObjCol.Add(Line1)
acDBObjCol.Add(Line2)
acDBObjCol.Add(Line3)
acDBObjCol.Add(Line4)
acDBObjCol.Add(Line5)
acDBObjCol.Add(Line6)
''MsgBox(Line1.EndPoint.X)
Dim myRegionCol As DBObjectCollection = New DBObjectCollection()
myRegionCol = Autodesk.AutoCAD.DatabaseServices.Region.CreateFromCurves(acDBObjCol)
Dim acRegion As Autodesk.AutoCAD.DatabaseServices.Region = DirectCast(myRegionCol(0), Autodesk.AutoCAD.DatabaseServices.Region)
acBlkTblRec.AppendEntity(acRegion)
acTrans.AddNewlyCreatedDBObject(acRegion, True)
' ---> here you might be want to erase and dispose lines after
acTrans.Commit()
End Using
Catch ex As System.Exception
MsgBox("Error" + vbLf + ex.ToString)
End Try
End Sub
~'J'~ 你好
我在开头创建的函数将线条绘制到当前UCS
然后我决定在代码中挤出这些项
如果我识别出绘制的点都是正确的,如果我在AutoCAD中手动选择要创建一个区域的项目,所有这些都有效。但代码中的相同项给出了创建区域的错误。
我知道的错误:1)我用来创建行的方法不完全正确,无法在代码中创建区域?2) 当我在代码中使用区域时,我可能不在正确的ucs上?
您的代码例程更加简单
将完成您的示例
感谢您的帮助
顺致敬意,
穴居人 顺便说一句,山洞不错 你好
在WCS中生成项目时,所有项目都是文件
如果UCS发生变化,则错误如下:
我学到的是,我没有将这些点转换为新用户UCS。
正在进行中
有什么好主意吗
原谅-第一次写代码来画东西-所以必须发现正确的方法
表示感谢
穴居人
密码文件 您的代码经过了适度的重构。我认为原作的一些转换是无序的。
重组。文件 哎呀。我忘了变换初始点。这将更好地匹配当前UCS原点。
<CommandMethod("demoreg")> PublicSub RegionDemo()
Dim acCurDb As Database= HostApplicationServices.WorkingDatabase
Dim ed As Editor =Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor
Try
Using acTrans As Autodesk.AutoCAD.DatabaseServices.Transaction =
acCurDb.TransactionManager.StartTransaction()
Dim acBlkTbl As Autodesk.AutoCAD.DatabaseServices.BlockTable
acBlkTbl =acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)
Dim acBlkTblRec As Autodesk.AutoCAD.DatabaseServices.BlockTableRecord
acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace),OpenMode.ForWrite)
Dim acDBObjCol As DBObjectCollection= New DBObjectCollection()
Dim MatToUcs As Matrix3d= GetUcsMatrix(acCurDb)
Dim Initial As Point3d= New Point3d().TransformBy(MatToUcs)
Dim Line1 As Line= DrawLine(Initial, 0.0, 100, MatToUcs)
Dim Line2 As Line= DrawLine(Line1.EndPoint, Math.PI / 2 - Math.PI / 6, 100, MatToUcs)
Dim Line3 As Line= DrawLine(Line2.EndPoint, Math.PI / 2 + Math.PI / 6, 100, MatToUcs)
Dim Line4 As Line= DrawLine(Line3.EndPoint, Math.PI, 100,MatToUcs)
Dim Line5 As Line= DrawLine(Line4.EndPoint, Math.PI * 1.5 - Math.PI / 6, 100, MatToUcs)
Dim Line6 As Line= DrawLine(Line5.EndPoint, Math.PI * 1.5 + Math.PI / 6, 100, MatToUcs)
acDBObjCol.Add(Line1)
acDBObjCol.Add(Line2)
acDBObjCol.Add(Line3)
acDBObjCol.Add(Line4)
acDBObjCol.Add(Line5)
acDBObjCol.Add(Line6)
Dim myRegionCol As DBObjectCollection= New DBObjectCollection()
myRegionCol = Autodesk.AutoCAD.DatabaseServices.Region.CreateFromCurves(acDBObjCol)
Dim acRegion As Autodesk.AutoCAD.DatabaseServices.Region = DirectCast(myRegionCol(0),Autodesk.AutoCAD.DatabaseServices.Region)
acBlkTblRec.AppendEntity(acRegion)
acTrans.AddNewlyCreatedDBObject(acRegion, True)
'---> here you might be want to erase and dispose lines after
acTrans.Commit()
End Using
Catch ex As System.Exception
MsgBox("Error"+ vbLf + ex.ToString)
End Try
End Sub
你好
非常感谢你的帮助
我的代码按预期运行。
“尺寸线1作为线=绘制线(首字母,0.0100,MatToUcs)”
您的方法与上面的高级方法一样优秀-使代码保持简单
现在,我们面临着下一个挑战
干杯
谢谢
页:
[1]