CaveMan Posted February 3, 2012 Posted February 3, 2012 :?Good Day Struggling a little with some code to create a region of various items: Code gives exception on the Red Highlited line: [size=2] [/size][size=2][color=#0000ff][size=2][color=#0000ff]Using[/color][/size][/color][/size][size=2] acTrans [/size][size=2][color=#0000ff][size=2][color=#0000ff]As[/color][/size][/color][/size][size=2] Autodesk.AutoCAD.DatabaseServices.Transaction = acCurDb.TransactionManager.StartTransaction() [/size][size=2][color=#0000ff][size=2][color=#0000ff]Dim[/color][/size][/color][/size][size=2] acBlkTbl [/size][size=2][color=#0000ff][size=2][color=#0000ff]As[/color][/size][/color][/size][size=2] Autodesk.AutoCAD.DatabaseServices.BlockTable acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) [/size][size=2][color=#0000ff][size=2][color=#0000ff]Dim[/color][/size][/color][/size][size=2] acBlkTblRec [/size][size=2][color=#0000ff][size=2][color=#0000ff]As[/color][/size][/color][/size][size=2] Autodesk.AutoCAD.DatabaseServices.BlockTableRecord acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), OpenMode.ForWrite) [/size][size=2][color=#0000ff][size=2][color=#0000ff]Dim[/color][/size][/color][/size][size=2] acDBObjCol [/size][size=2][color=#0000ff][size=2][color=#0000ff]As[/color][/size][/color][/size][size=2] DBObjectCollection = [/size][size=2][color=#0000ff][size=2][color=#0000ff]New[/color][/size][/color][/size][size=2] DBObjectCollection() acDBObjCol.Add(Line1) acDBObjCol.Add(Line2) acDBObjCol.Add(Line3) acDBObjCol.Add(Line4) acDBObjCol.Add(Line5) acDBObjCol.Add(Line6) MsgBox(Line1.EndPoint.X) [/size][size=2][color=#0000ff][size=2][color=#0000ff]Dim[/color][/size][/color][/size][size=2] myRegionCol [/size][size=2][color=#0000ff][size=2][color=#0000ff]As[/color][/size][/color][/size][size=2] DBObjectCollection = [/size][size=2][color=#0000ff][size=2][color=#0000ff]New[/color][/size][/color][/size][size=2] DBObjectCollection() [color=red]myRegionCol = Autodesk.AutoCAD.DatabaseServices.Region.CreateFromCurves(acDBObjCol)[/color] [/size][size=2][color=#0000ff][size=2][color=#0000ff]Dim[/color][/size][/color][/size][size=2] acRegion [/size][size=2][color=#0000ff][size=2][color=#0000ff]As[/color][/size][/color][/size][size=2] Autodesk.AutoCAD.DatabaseServices.Region = myRegionCol(0) acBlkTblRec.AppendEntity(acRegion) acTrans.AddNewlyCreatedDBObject(acRegion, [/size][size=2][color=#0000ff][size=2][color=#0000ff]True[/color][/size][/color][/size][size=2]) [/size][size=2][color=#0000ff][size=2][color=#0000ff]End[/color][/size][/color][/size][size=2] [/size][size=2][color=#0000ff][size=2][color=#0000ff]Using [/color][/size][/color][/size][size=2][/size][size=2][color=#0000ff][size=2][color=#0000ff]Catch[/color][/size][/color][/size][size=2] ex [/size][size=2][color=#0000ff][size=2][color=#0000ff]As[/color][/size][/color][/size][size=2] Exception MsgBox([/size][size=2][color=#a31515][size=2][color=#a31515]"Error"[/color][/size][/color][/size][size=2]) [/size][size=2][color=#0000ff][size=2][color=#0000ff]End[/color][/size][/color][/size][size=2] [/size][size=2][color=#0000ff][size=2][color=#0000ff]Try [/color][/size][/color][/size] The Lines Are cteated with this Public Function: "Friend Function DrawLine" which returns the line. Lines Generated as such: " Line1 = DrawLine(PntSt, ModDrawLine.DTR(AngLine), LineLng)" [size=2] [/size][size=2][color=#0000ff][size=2][color=#0000ff]Friend[/color][/size][/color][/size][size=2] [/size][size=2][color=#0000ff][size=2][color=#0000ff]Function[/color][/size][/color][/size][size=2] DrawLine([/size][size=2][color=#0000ff][size=2][color=#0000ff]ByVal[/color][/size][/color][/size][size=2] startPoint [/size][size=2][color=#0000ff][size=2][color=#0000ff]As[/color][/size][/color][/size][size=2] Point3d, [/size][size=2][color=#0000ff][size=2][color=#0000ff]ByVal[/color][/size][/color][/size][size=2] angle [/size][size=2][color=#0000ff][size=2][color=#0000ff]As[/color][/size][/color][/size][size=2] [/size][size=2][color=#0000ff][size=2][color=#0000ff]Double[/color][/size][/color][/size][size=2], [/size][size=2][color=#0000ff][size=2][color=#0000ff]ByVal[/color][/size][/color][/size][size=2] length [/size][size=2][color=#0000ff][size=2][color=#0000ff]As[/color][/size][/color][/size][size=2] [/size][size=2][color=#0000ff][size=2][color=#0000ff]Double[/color][/size][/color][/size][size=2]) [/size][size=2][color=#0000ff][size=2][color=#0000ff]As[/color][/size][/color][/size][size=2] Line [/size][size=2][color=#0000ff][size=2][color=#0000ff]Dim[/color][/size][/color][/size][size=2] acDoc [/size][size=2][color=#0000ff][size=2][color=#0000ff]As[/color][/size][/color][/size][size=2] Document = Application.DocumentManager.MdiActiveDocument [/size][size=2][color=#0000ff][size=2][color=#0000ff]Dim[/color][/size][/color][/size][size=2] acCurDb [/size][size=2][color=#0000ff][size=2][color=#0000ff]As[/color][/size][/color][/size][size=2] Database = acDoc.Database acDoc.LockDocument() [/size][size=2][color=#0000ff][size=2][color=#0000ff]Dim[/color][/size][/color][/size][size=2] id [/size][size=2][color=#0000ff][size=2][color=#0000ff]As[/color][/size][/color][/size][size=2] ObjectId [/size][size=2][color=#0000ff][size=2][color=#0000ff]Dim[/color][/size][/color][/size][size=2] line [/size][size=2][color=#0000ff][size=2][color=#0000ff]As[/color][/size][/color][/size][size=2] Line = [/size][size=2][color=#0000ff][size=2][color=#0000ff]Nothing [/color][/size][/color][/size][size=2][/size][size=2][color=#0000ff][size=2][color=#0000ff]Using[/color][/size][/color][/size][size=2] db [/size][size=2][color=#0000ff][size=2][color=#0000ff]As[/color][/size][/color][/size][size=2] Database = HostApplicationServices.WorkingDatabase() [/size][size=2][color=#0000ff][size=2][color=#0000ff]Dim[/color][/size][/color][/size][size=2] endpoint [/size][size=2][color=#0000ff][size=2][color=#0000ff]As[/color][/size][/color][/size][size=2] Point3d = ModDrawLine.PolarPoint(startPoint, angle, length) startPoint = ModDrawLine.TransformByUCS(startPoint, db) endpoint = ModDrawLine.TransformByUCS(endpoint, db) [/size][size=2][color=#0000ff][size=2][color=#0000ff]Using[/color][/size][/color][/size][size=2] tr [/size][size=2][color=#0000ff][size=2][color=#0000ff]As[/color][/size][/color][/size][size=2] Transaction = db.TransactionManager.StartTransaction [/size][size=2][color=#0000ff][size=2][color=#0000ff]Try [/color][/size][/color][/size][size=2][/size][size=2][color=#0000ff][size=2][color=#0000ff]Dim[/color][/size][/color][/size][size=2] btr [/size][size=2][color=#0000ff][size=2][color=#0000ff]As[/color][/size][/color][/size][size=2] BlockTableRecord = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite, [/size][size=2][color=#0000ff][size=2][color=#0000ff]False[/color][/size][/color][/size][size=2]) line = [/size][size=2][color=#0000ff][size=2][color=#0000ff]New[/color][/size][/color][/size][size=2] Line(startPoint, endpoint) id = btr.AppendEntity(line) db.TransactionManager.AddNewlyCreatedDBObject(line, [/size][size=2][color=#0000ff][size=2][color=#0000ff]True[/color][/size][/color][/size][size=2]) tr.Commit() [/size][size=2][color=#0000ff][size=2][color=#0000ff]Catch[/color][/size][/color][/size][size=2] ex [/size][size=2][color=#0000ff][size=2][color=#0000ff]As[/color][/size][/color][/size][size=2] Exception tr.Abort() [/size][size=2][color=#0000ff][size=2][color=#0000ff]End[/color][/size][/color][/size][size=2] [/size][size=2][color=#0000ff][size=2][color=#0000ff]Try [/color][/size][/color][/size][size=2][/size][size=2][color=#0000ff][size=2][color=#0000ff]End[/color][/size][/color][/size][size=2] [/size][size=2][color=#0000ff][size=2][color=#0000ff]Using [/color][/size][/color][/size][size=2][/size][size=2][color=#0000ff][size=2][color=#0000ff]End[/color][/size][/color][/size][size=2] [/size][size=2][color=#0000ff][size=2][color=#0000ff]Using [/color][/size][/color][/size][size=2][/size][size=2][color=#0000ff][size=2][color=#0000ff]Return[/color][/size][/color][/size][size=2] line [/size][size=2][color=#0000ff][size=2][color=#0000ff]End[/color][/size][/color][/size][size=2] [/size][size=2][color=#0000ff][size=2][color=#0000ff]Function [/color][/size][/color][/size] Any help welcome please Regards CaveMan Code Quote
fixo Posted February 3, 2012 Posted February 3, 2012 I'm pretty sure your lines didn't create closed area, because this is working code To make sureyou better use closed polyline instead or draw lines chainage dependend on previous line points, e.g.: ' 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'~ Quote
CaveMan Posted February 4, 2012 Author Posted February 4, 2012 Hi There The function i create in the beginning Drew the lines to the current UCS I then decided to want to extrude the items in code If i ID'ed the drawn points all were correct, If i manually in AutoCAD i selected the items to cteate a region all worked. But the same items in code give the error to create the region. Error i know of: 1) Method i used to create the lines not 100% correct to create a region in code? 2) When i used region in code i may not be on the correct ucs? Your routine of code more simple Will work through your example Appreciate the assistance Best Regards CaveMan Quote
CaveMan Posted February 4, 2012 Author Posted February 4, 2012 Hi when the item is generated in WCS all is file If the UCS is changed then Error As Below: what i have picked up is that i have not transformed the points to the new User UCS. in progress Any ideas most welcome Forgive - first time writing code to draw something - so have to discover the correct methology In Appreciation CaveMan Code.doc Quote
SEANT Posted February 4, 2012 Posted February 4, 2012 Your code mildly restructured. I think the original had some transformations out of sequence. Restructured.doc Quote
SEANT Posted February 4, 2012 Posted February 4, 2012 (edited) Oops. I forgot to transform the initial point. This should provide a better match with the current UCS origin. <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 Edited February 4, 2012 by SEANT Quote
CaveMan Posted February 6, 2012 Author Posted February 6, 2012 Hi There Thanks a lot for the assistance My code functioning to what was desired. "Dim Line1 As Line= DrawLine(Initial, 0.0, 100, MatToUcs)" Your method excellent as highlited above - keeps code simple Now stuck on the next challenges Cheers Thanks a Lot Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.