Dim Varao As Autodesk.AutoCAD.DatabaseServices.Polyline = New Autodesk.AutoCAD.DatabaseServices.Polyline() If ComboBox1.SelectedIndex = 0 Then Varao.SetDatabaseDefaults() Varao.AddVertexAt(0, New Point2d(0+0.025, 0), 1, (0.02), (0.02)) Varao.AddVertexAt(1, New Point2d(0-0.025, 0), 1, (0.02), (0.02)) Varao.Closed = True ms.AppendEntity(Varao) trans.AddNewlyCreatedDBObject(Varao, True) End If Dim Count As Integer = 1 Dim dAng As Double = (2 * PI) / TextBox1.Text Dim PontoRotacao As Point2d = New Point2d(pontoinsert.X, pontoinsert.Y) While (Count < TextBox1.Text) Dim VaraoClone As Entity = Varao.Clone() Dim acExts As Extents3d Dim acPtObjBase As Point2d 'I have here is the problem (circle) Dim ArrayFerro As Circle = VaraoClone If IsDBNull(ArrayFerro) = False Then acPtObjBase = New Point2d(ArrayFerro.Center.X, ArrayFerro.Center.Y) Else acExts = VaraoClone.Bounds.GetValueOrDefault() acPtObjBase = New Point2d(acExts.MinPoint.X, acExts.MaxPoint.Y) End If Dim dDist As Double = PontoRotacao.GetDistanceTo(acPtObjBase) Dim dAngFromX As Double = PontoRotacao.GetVectorTo(acPtObjBase).Angle Dim acPt2dTo As Point2d = PolarPoints(PontoRotacao, (Count * dAng) + dAngFromX, dDist) Dim acVec2d As Vector2d = acPtObjBase.GetVectorTo(acPt2dTo) Dim acVec3d As Vector3d = New Vector3d(acVec2d.X, acVec2d.Y, 0) VaraoClone.TransformBy(Matrix3d.Displacement(acVec3d)) ms.AppendEntity(VaraoClone) trans.AddNewlyCreatedDBObject(VaraoClone, True) Count = Count + 1 End While
my problem Dim ArrayFerro As Circle = VaraoClone
System.InvalidCastException: Unable to cast object of type 'Autodesk.AutoCAD.DatabaseServices.Polyline' to type 'Autodesk.AutoCAD.DatabaseServices.Circle'.
Solved! Go to Solution.
Solved by Alfred.NESWADBA. Go to Solution.
Hi,
the errormessage tells what's wrong!
You declared a variable to be able hold an object of type DatabaseServices.Circle
Dim ArrayFerro As Circle
.... and then you try to store an object of type DatabaseServices.Polyline in it.
ArrayFerro = VaraoClone
Well you wrote all in one statement, I splitted it to declare what part of statement is what part of my description.
And a polyline with two vertices, one arc and option closed may look like a circle, but does not make an object of type circle 😉
- alfred -
aand what is the solution? if you please
Hi,
>> and what is the solution? if you please
There is no solution except of "you cannot cast an object of type DatabaseServices.Polyine to type DatabaseServices.Circle"
I see your code as a sample you built to show something as code-snippet (and not to bypass your full application, I think) but I don't understand what this code should do, sorry.
I first don't understand why you draw a Polyline to get a shape like a circle (and then need a circle), why not starting with a circle?
If your situation is that you have already a Polyline existing in the drawing-database, then you have to check the polyline itself, not every polyline has arcs, so even splitting the polyline into it's parts (and then having lines and/or arcs) you will not get a Center.X or Center.Y for line-segments.
I want to help, but sorry that I don't understand what your goal is. Could you please descibe it? What do you have (input parameters or existing objects) and what do you want to get. If it is hard to describe ==> upload drawings.
- alfred -
hi,
What I need is a thread to draw a donut and then draw a Polar Array of it around a centre point that I can designate.
Thanks
Hi,
take a look to this, hope that's what you were looking for (and if not that you can adapt parts of it to your needs)
Imports Autodesk.AutoCAD.ApplicationServices Imports Autodesk.AutoCAD.DatabaseServices Imports Autodesk.AutoCAD.Geometry Imports Autodesk.AutoCAD.EditorInput Public Class cAutodeskForum2011 Public Shared Function createArrayOfDonuts() As Boolean Dim tRetVal As Boolean = False Dim tAcadDoc As Document = Nothing Dim tAcadDocLock As DocumentLock = Nothing Dim tTrAct As Transaction = Nothing Try tAcadDoc = Application.DocumentManager.MdiActiveDocument tAcadDocLock = tAcadDoc.LockDocument tTrAct = tAcadDoc.TransactionManager.StartTransaction Dim tGoOn As Boolean = True 'get Center of polar array Dim tPos As Point3d If tGoOn Then Dim tPntGet As PromptPointResult = tAcadDoc.Editor.GetPoint(vbNewLine & "Center of polar array: ") If tPntGet.Status = PromptStatus.OK Then tPos = tPntGet.Value Else tGoOn = False tAcadDoc.Editor.WriteMessage("Center of Array: input canceled") End If End If 'get Radius of polar array Dim tRad As Double = 0.0 If tGoOn Then Dim tDistOpts As PromptDistanceOptions = New PromptDistanceOptions(vbNewLine & "Radius of polar array: ") tDistOpts.AllowNegative = False tDistOpts.AllowNone = False tDistOpts.AllowZero = False tDistOpts.BasePoint = tPos tDistOpts.UseBasePoint = True Dim tDblGet As PromptDoubleResult = tAcadDoc.Editor.GetDistance(tDistOpts) If tDblGet.Status = PromptStatus.OK Then tRad = tDblGet.Value Else tGoOn = False tAcadDoc.Editor.WriteMessage("Radius of polar array: input canceled") End If End If 'you can now also get these values by requesting the user or by passing as parameters to this function Dim tStartAngle As Double = 0 Dim tEndAngle As Double = Math.PI * 2 Dim tNumberOfItems As Double = 20 Dim tDonutRadius As Double = tRad / 10.0 Dim tDonutPLineWidth As Double = tRad / 100.0 If tGoOn Then Dim tDonutBasePoly As Polyline = getDonut(tDonutRadius, tDonutPLineWidth) If tDonutBasePoly IsNot Nothing Then 'move the basepoly from 0,0,0 (where it is created) to the center of the array Dim tBaseMat As Matrix3d = Matrix3d.Displacement(tPos.GetAsVector) tDonutBasePoly.TransformBy(tBaseMat) 'ok, seems to have all to draw the objects 'get modelspace or paperspace, whatever is current Dim tSpace As BlockTableRecord = CType(tTrAct.GetObject(tAcadDoc.Database.CurrentSpaceId, OpenMode.ForWrite), BlockTableRecord) For i As Double = tStartAngle To tEndAngle Step ((tEndAngle - tStartAngle) / tNumberOfItems) Dim tNewPoly As Polyline = CType(tDonutBasePoly.Clone, Polyline) Dim tMat As Matrix3d = Matrix3d.Displacement(New Vector3d(tRad * Math.Sin(i), tRad * Math.Cos(i), 0)) tNewPoly.TransformBy(tMat) tSpace.AppendEntity(tNewPoly) tTrAct.AddNewlyCreatedDBObject(tNewPoly, True) Next tTrAct.Commit()
tRetVal = true
'clean up memory tDonutBasePoly.Dispose() tDonutBasePoly = Nothing Else 'should not land here! End If End If Catch ex As Exception Call MsgBox("Error occured!" & vbNewLine & ex.Message) Finally If tTrAct IsNot Nothing Then tTrAct.Dispose() : tTrAct = Nothing If tAcadDocLock IsNot Nothing Then tAcadDocLock.Dispose() : tAcadDocLock = Nothing End Try Return tRetVal End Function Private Shared Function getDonut(ByVal Rad As Double, ByVal PlineWidth As Double) As Polyline Dim tPoly As Polyline = Nothing Try tPoly = New Polyline tPoly.SetDatabaseDefaults() tPoly.AddVertexAt(0, New Point2d(Rad, 0), 1, PlineWidth, PlineWidth) tPoly.AddVertexAt(0, New Point2d(-Rad, 0), 1, PlineWidth, PlineWidth) tPoly.Closed = True Catch ex As Exception If tPoly IsNot Nothing Then tPoly.Dispose() : tPoly = Nothing Throw New Exception("Error creating Polyline in function 'drawDonut'", ex) End Try Return tPoly End Function End Class
HTH, - alfred -
thanks, but i need a solution for my thread.