I've recently ported some code from VS2008 and AutoCAD 2010 and realized that the eNotNewlyCreated exception continues to be raised when inserting blocks in a loop using AddNewlyCreatedObject in code that I'm pretty sure is only being called once.
Does anyone know anything about this?
Solved! Go to Solution.
Solved by adam.nagy. Go to Solution.
Hi there,
Are you saying that before migrating your code to VS2010 & ACAD 2012 everything was fine with your code?
I've only seen this error coming up when the developer was actually trying to add the same new object to the transaction twice. Or maybe you are opening an object from the database and somehow your code ends up in the section when you try to add it to the transaction? :-s
Good luck finding the issue!
Cheers,
Adam Nagy
Autodesk Developer Network
Yes the code was the same. I've stepped through everything right up until the error. Ill enclose the code should someone have the time or inclination to take a peak at it. It inserts paving stone blocks at the appropritae angle along a closed LW Polyline.
Best regards,
<CommandMethod("rdap")> _ Public Sub RefDistAlongPath(ByVal asBlkName As String, ByVal bo As Double, ByVal PaverWid As Double, Optional ByVal DbType As String = "") Dim myDB As Database = HostApplicationServices.WorkingDatabase Using myTrans As Transaction = myDB.TransactionManager.StartTransaction Dim myEd As Editor = DocumentManager.MdiActiveDocument.Editor 'Dim myPEO As New PromptEntityOptions("Select a 2D Polyline:") 'myPEO.SetRejectMessage("You must select a 2D Polyline." & vbCrLf) 'myPEO.AddAllowedClass(GetType(Polyline), False) Dim myPER As PromptEntityResult = GetEntityWithAllowedClass() 'myEd.GetEntity(myPEO) Dim Paver1 As New Paver Dim OffsetDist As Double ''TODO: Add error handling for open polylines and blockname not present Select Case myPER.Status Case EditorInput.PromptStatus.OK Dim myOID As ObjectId = myPER.ObjectId Dim myPL1 As Polyline = myTrans.GetObject(myOID, OpenMode.ForRead, False) Dim myPL2 As Polyline = myTrans.GetObject(myOID, OpenMode.ForWrite, False) Dim myCrv As Curve = myTrans.GetObject(myOID, OpenMode.ForRead, False) Paver1.Series = asBlkName 'If (!myBlkTable.Has(asBlkName)) Then ' { ' ed.WriteMessage ("Current Database does not contain a block of that name!"); ' return; ' } Dim myBlkTable As BlockTable = myTrans.GetObject(myDB.BlockTableId, OpenMode.ForRead) Dim myBlkObject As BlockTableRecord = myTrans.GetObject(myBlkTable(asBlkName), OpenMode.ForRead) Dim myPLLeng As Double = myPL1.Length Dim numsegs As Int16 = 0 Dim segs() As Seg Dim sb As StringBuilder = New StringBuilder() Dim pt0 As Point2d = myPL1.GetPoint2dAt(0) Dim i As Int16 For i = 0 To myPL1.NumberOfVertices - 1 ReDim Preserve segs(i) If myPL1.GetSegmentType(i) = SegmentType.Line Then Dim test As LineSegment2d = myPL1.GetLineSegment2dAt(i) Dim d As Double = test.Length Dim pt As Point2d = test.EndPoint Dim vec As Vector2d = test.Direction Dim ang As Double = vec.Angle Dim azimuth As Double = 0 If (ang = 0) Then azimuth = 90 End If If (ang > 0) And (ang < (PI / 2.0)) Then azimuth = 90 - RadianToDegree(ang) End If If (ang = (PI / 2.0)) Then azimuth = 0.0 End If If (ang > (PI / 2.0)) And (ang < PI) Then azimuth = (180 - RadianToDegree(ang)) + 270 End If If (ang = PI) Then azimuth = 270.0 End If If (ang > PI) And (ang < (PI * 1.5)) Then azimuth = (270 - RadianToDegree(ang)) + 180 End If If (ang = (PI * 1.5)) Then azimuth = 180.0 End If If (ang > (PI * 1.5)) And (ang < (PI * 2.0)) Then azimuth = (360 - RadianToDegree(ang)) + 90 End If If (ang = (PI * 2.0)) Then azimuth = 90.0 End If 'myEd.WriteMessage("Segment Length #" & (i + 1) & " = " & d & "\nAzimuth = " & azimuth & "\n") segs(i).Length = d segs(i).Azimuth = azimuth ElseIf myPL1.GetSegmentType(i) = SegmentType.Arc Then Dim test As CircularArc2d = myPL1.GetArcSegment2dAt(i) Dim interval_of_arc As Interval = test.GetInterval() Dim length_of_arc As Double = test.GetLength(0.0, interval_of_arc.Element) 'myEd.WriteMessage("Segment Length #" & (i + 1) & " = " & length_of_arc & "and it is an arc.") '' NOT WORKING segs(i).Length = length_of_arc segs(i).Azimuth = -1.0 End If Next numsegs = segs.GetUpperBound(0) Dim acDBObjColl As DBObjectCollection = New DBObjectCollection() acDBObjColl.Add(myPL1) 'Calculate centroid of Polyline Dim myCent As Point3d = makeSolid(myPL1) Dim iTop As Int16 = Decimal.Divide(myPLLeng, (PaverWid + bo_adj)) - 2 Dim acPoint As DBPoint = New DBPoint(New Point3d(myCent.X, myCent.Y, 0)) acPoint.SetDatabaseDefaults() Dim myCurrSpace As BlockTableRecord = myTrans.GetObject(myDB.CurrentSpaceId, OpenMode.ForWrite) myPL2.Layer = "Hatch" '' Add the new object to the block table record and the transaction myCurrSpace.AppendEntity(acPoint) 'myTrans.AddNewlyCreatedDBObject(acPoint, True) '' Set the style for all point objects in the drawing myDB.Pdmode = 34 myDB.Pdsize = 1 Dim MyPoints As DBPoint = acPoint Dim myStdP1_3d As Point3d = myCrv.GetPointAtDist(0) Dim reset_start As Double For j = 0 To numsegs Dim myP1_3d As Point3d Dim myAdjP13D As Point3d Dim lenper As Int16 = 0 CalcTurnAdj(segs, j, (PaverWid + bo_adj)) Dim iper As Int16 = ((segs(j).Length + CurrLeng_Adj) \ (PaverWid + bo_adj)) Dim rema As Double = Decimal.op_Modulus((segs(j).Length + CurrLeng_Adj), (PaverWid + bo_adj)) 'Debug.Print(rema.ToString) For i = 1 To iper ' IIf(String.Compare(asBlkName, "Old Country Stone") = 0, iper + 1, iper) 'rema_adj = (PaverWid / 2.0) myP1_3d = myCrv.GetPointAtDist(reset_start + ((PaverWid + bo_adj) * IIf(String.Compare(asBlkName, "Standard") = 0, i - 1, i - 0.5)) + ap_adj) ' - IIf(i = iper + 1, rema_adj, 0)) myAdjP13D = New Point3d(myP1_3d.X + St_Pt_AdjX, myP1_3d.Y + St_Pt_AdjY, 0) 'FIND OUT OUT TO DO THIS!!! Dim myV1_3d As Vector3d = myCrv.GetFirstDerivative(myP1_3d) Dim myBlkAngle As Double = Math.Atan(myV1_3d.Y / myV1_3d.X) myP1_3d = myAdjP13D Dim dmlPointA As DBPoint = New DBPoint(New Point3d(myP1_3d.X, myP1_3d.Y, 0)) 'myCurrSpace.AppendEntity(dmlPointA) Dim tmpDist, tmpDist2 As Double tmpDist = MyPoints.Position.DistanceTo(dmlPointA.Position) Dim myBlKPt As DBPoint = New DBPoint(PolarPoints(dmlPointA.Position, myBlkAngle + Decimal.Divide(PI, 2.0), bo + bo_adj)) Dim dmlPoint As DBPoint = myBlKPt tmpDist2 = MyPoints.Position.DistanceTo(dmlPoint.Position) If String.Compare(asBlkName, "Standard") = 0 And String.Compare(DbType.Substring(0, 3), "Cal") <> 0 Or _ String.Compare(asBlkName, "AP Std") = 0 Then myBlkAngle = myBlkAngle + Decimal.Divide(PI, 2.0) End If If tmpDist2 > tmpDist Then myBlkAngle = myBlkAngle + PI End If OffsetDist = dmlPointA.Position.DistanceTo(myBlKPt.Position) Dim myBlkInsert As BlockReference = New BlockReference(myP1_3d, myBlkObject.ObjectId) myBlkInsert.Rotation = myBlkAngle myCurrSpace.AppendEntity(myBlkInsert) myBlkInsert.SetDatabaseDefaults() 'Manage Dynamic Block Capabilities Here If myBlkObject.IsDynamicBlock = True Then Dim v As DynamicBlockReferencePropertyCollection = myBlkInsert.DynamicBlockReferencePropertyCollection Dim prop As DynamicBlockReferenceProperty For Each prop In v Dim mystr As String = prop.PropertyName Select Case mystr Case "Length" prop.Value = bo Case "Width" prop.Value = PaverWid Case "Distance2" ' Old Country Stone prop.Value = bo Case "Remainder" ' Old Country Stone prop.Value = IIf(i = iper + 1, rema, 178.0) Case "Complexity" ' AP STD prop.Value = DbType Case "Visibility1" ' Standard and Chateau and Villa prop.Value = DbType End Select Next End If myBlkInsert.Layer = "Objects_3" dmlPointA.Dispose() dmlPoint.Dispose() myBlKPt.Dispose() myTrans.AddNewlyCreatedDBObject(myBlkInsert, True) Next i reset_start = reset_start + segs(j).Length Next myCrv.Dispose() Dim myDbObjColl2 As DBObjectCollection = myPL1.GetOffsetCurves(IIf(IsCCW(myPL1), -OffsetDist, OffsetDist)) '(IIf((bo + bo_adj) > 0, 0 - (bo + bo_adj), (bo + bo_adj))) For Each myEntity As Entity In myDbObjColl2 '' Add each offset object myCurrSpace.AppendEntity(myEntity) myEntity.Layer = "Hatch" myTrans.AddNewlyCreatedDBObject(myEntity, True) '' Adds the Pline to an object id array Dim myObjIdColl3 As ObjectIdCollection = New ObjectIdCollection() myObjIdColl3.Add(myEntity.ObjectId) '' Create the hatch object and append it to the block table record Dim myHatch As Hatch = New Hatch() myCurrSpace.AppendEntity(myHatch) myTrans.AddNewlyCreatedDBObject(myHatch, True) '' Set the properties of the hatch object '' Associative must be set after the hatch object is appended to the
You should always call AddNewlyCreatedDBObject() right after adding the entity to the database (i.e. calling AppendEntity) - as you are doing it in case of the hatch entity. Once the transaction knows about it you don't even have to call dispose on the entity, though calling Dispose() should not do any harm.
Also, I would suggest splitting up your function into subfunctions because I think it went way beyond the line count that can be QA'd efficiently. 🙂
And I would use "Using" keyword on the objects instead of calling Dispose() explicitly. I think it makes the code look more organized and it's also useful since even if somewhere you get an exception the Dispose() will still be called implicitly by Using.
I did take your suggestion which fixed the problem right away. For some reason this "unreccomended" code works absolutely fine in earler versions of MS Studio and AutoCAD.
Can't find what you're looking for? Ask the community or share your knowledge.