<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Help needed in slicing solids at given intervals in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/help-needed-in-slicing-solids-at-given-intervals/m-p/5643374#M40019</link>
    <description>Hi!, First I tried with one transaction only. But if I commit the transaction, I couldn't iterate through the lines for further processing. If possible to iterate, please let me know where to commit the transaction.&lt;BR /&gt;&lt;BR /&gt;Regards,&lt;BR /&gt;Srikanth.</description>
    <pubDate>Wed, 20 May 2015 13:37:59 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2015-05-20T13:37:59Z</dc:date>
    <item>
      <title>Help needed in slicing solids at given intervals</title>
      <link>https://forums.autodesk.com/t5/net-forum/help-needed-in-slicing-solids-at-given-intervals/m-p/5637766#M40015</link>
      <description>&lt;P&gt;Hi!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to iterate through the user selected cut lines and for every cutline, identify the solids crossing and slice them to that line. Below the code is working for single line selection. But for multiple lines selection, in the second iteration, getting the error.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help would be greatly appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Image: &lt;/STRONG&gt;User Selecting Lines &amp;amp; Solids to Identify&lt;/P&gt;&lt;P&gt;&lt;IMG src="https://forums.autodesk.com/t5/image/serverpage/image-id/168779iC1BEED5A1420025D/image-size/original?v=mpbl-1&amp;amp;px=-1" title="Slice Solid.png" alt="Slice Solid.png" border="0" /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Code:&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE&gt;Public Sub SliceSolid()
        Dim Doc As Document = Application.DocumentManager.MdiActiveDocument
        Dim DB As Database = Doc.Database
        Dim Ed As Editor = Doc.Editor
        Try
            Using Tr As Transaction = DB.TransactionManager.StartTransaction
                Dim BT As BlockTable = Tr.GetObject(DB.BlockTableId, OpenMode.ForRead)
                Dim BTR As BlockTableRecord = Tr.GetObject(BT(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
                Dim SObjPrRes As PromptSelectionResult = Ed.GetSelection
                If SObjPrRes.Status = PromptStatus.OK Then
                    Dim SSet As SelectionSet = SObjPrRes.Value
                    For Each Obj As SelectedObject In SSet
                        Dim Ent As Entity = Tr.GetObject(Obj.ObjectId, OpenMode.ForRead)
                        If Obj.ObjectId.ObjectClass.DxfName = "LINE" Then
                            Dim L As Line : L = Ent
                            Dim PT1 As Point3d = L.StartPoint : Dim PT2 As Point3d = L.EndPoint
                            Ed.WriteMessage(vbLf &amp;amp; "Line Start : " &amp;amp; L.StartPoint.ToString &amp;amp; " - Line End :" &amp;amp; L.EndPoint.ToString)
                            Dim CrossSelection As SelectionSet = SelectObjectsByCrossingWindow(L.StartPoint, L.EndPoint)
                            'PrintSelectionSetObjectIds(CrossSelection)
                            'SliceSolidsByPoints(CrossSelection, L.StartPoint, L.EndPoint)
                            Using Tr2 As Transaction = DB.TransactionManager.StartTransaction
                                For Each Obj2 As SelectedObject In CrossSelection
                                    If Obj2.ObjectId.ObjectClass.DxfName = "3DSOLID" Then
                                        Dim Sol3D As Solid3d = Tr2.GetObject(Obj2.ObjectId, OpenMode.ForWrite)
                                        Dim Pt3 As Point3d = New Point3d(PT1.X, PT1.Y, PT1.Z)
                                        Dim Pt4 As Point3d = New Point3d(PT1.X, PT1.Y, PT1.Z + 10)
                                        Dim Pt5 As Point3d = New Point3d(PT2.X, PT2.Y, PT1.Z + 10)
                                        Dim acPlane As Plane = New Plane(Pt3, Pt4, Pt5)
                                        Dim Sol3DSlice As Solid3d = Sol3D.Slice(acPlane, True)
                                        BTR.AppendEntity(Sol3DSlice)
                                        Tr2.AddNewlyCreatedDBObject(Sol3DSlice, True)
                                        Ed.WriteMessage(vbLf &amp;amp; "New Solid Created: " &amp;amp; Sol3DSlice.ObjectId.ToString())
                                    End If
                                Next
                                Tr2.Commit()
                            End Using
                        End If
                    Next
                End If
                Tr.Commit()
            End Using
        Catch ex As Exception
            MsgBox(ex.ToString, , "Sub - " &amp;amp; System.Reflection.MethodBase.GetCurrentMethod.Name)
            Clipboard.SetText(ex.ToString)
        End Try
    End Sub&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Error:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;System.InvalidOperationException: Operation is not valid due to the current state of the object.&lt;BR /&gt;at Autodesk.AutoCAD.Runtime.DisposableWrapper.Attach(IntPtr unmanagedPointer, Boolean autoDelete)&lt;BR /&gt;at Autodesk.AutoCAD.DatabaseServices.Solid3d.Slice(Plane plane, Boolean negativeHalfToo)&lt;BR /&gt;at ClassLibrary1.SliceSolids.SliceSolid()&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 16 May 2015 10:15:53 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/help-needed-in-slicing-solids-at-given-intervals/m-p/5637766#M40015</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2015-05-16T10:15:53Z</dc:date>
    </item>
    <item>
      <title>Re: Help needed in slicing solids at given intervals</title>
      <link>https://forums.autodesk.com/t5/net-forum/help-needed-in-slicing-solids-at-given-intervals/m-p/5639931#M40016</link>
      <description>From the code, looks like you're trying to slice the same solid twice? I believe that may fail as the solid is not there anymore...</description>
      <pubDate>Mon, 18 May 2015 13:53:31 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/help-needed-in-slicing-solids-at-given-intervals/m-p/5639931#M40016</guid>
      <dc:creator>augusto.goncalves</dc:creator>
      <dc:date>2015-05-18T13:53:31Z</dc:date>
    </item>
    <item>
      <title>Re: Help needed in slicing solids at given intervals</title>
      <link>https://forums.autodesk.com/t5/net-forum/help-needed-in-slicing-solids-at-given-intervals/m-p/5642832#M40017</link>
      <description>&lt;P&gt;Hi Augusto,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your response.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You are correct, if we cut the same solid twice without updating database. But in current case after slicing the solid, committed the transaction to update the database. After that program returns to next Cut line&amp;nbsp;and&amp;nbsp;identifies crossing solids (including newly cut solid) to process further.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Could you please confirm whether slicing is an asynchronous process and causing error? If so, what is the easiest way to overcome?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Srikanth.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 20 May 2015 05:19:17 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/help-needed-in-slicing-solids-at-given-intervals/m-p/5642832#M40017</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2015-05-20T05:19:17Z</dc:date>
    </item>
    <item>
      <title>Re: Help needed in slicing solids at given intervals</title>
      <link>https://forums.autodesk.com/t5/net-forum/help-needed-in-slicing-solids-at-given-intervals/m-p/5643148#M40018</link>
      <description>But note the top transaction is not committing, so that can cause the issue. From your error message and code, that can be the issue. Can you try with only 1 transaction that commit on every slice?</description>
      <pubDate>Wed, 20 May 2015 11:35:34 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/help-needed-in-slicing-solids-at-given-intervals/m-p/5643148#M40018</guid>
      <dc:creator>augusto.goncalves</dc:creator>
      <dc:date>2015-05-20T11:35:34Z</dc:date>
    </item>
    <item>
      <title>Re: Help needed in slicing solids at given intervals</title>
      <link>https://forums.autodesk.com/t5/net-forum/help-needed-in-slicing-solids-at-given-intervals/m-p/5643374#M40019</link>
      <description>Hi!, First I tried with one transaction only. But if I commit the transaction, I couldn't iterate through the lines for further processing. If possible to iterate, please let me know where to commit the transaction.&lt;BR /&gt;&lt;BR /&gt;Regards,&lt;BR /&gt;Srikanth.</description>
      <pubDate>Wed, 20 May 2015 13:37:59 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/help-needed-in-slicing-solids-at-given-intervals/m-p/5643374#M40019</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2015-05-20T13:37:59Z</dc:date>
    </item>
    <item>
      <title>Re: Help needed in slicing solids at given intervals</title>
      <link>https://forums.autodesk.com/t5/net-forum/help-needed-in-slicing-solids-at-given-intervals/m-p/5646279#M40020</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;So I did a further investigation on this. &lt;BR /&gt;&lt;BR /&gt;The DisposableWrapper will throw this InvalidOperation exception if the object being created is NULL/Nothing (among other problems). But as this is happening inside the API, it's not on your code. &lt;BR /&gt;&lt;BR /&gt;If you look at the C++ documentation, the SLICE method will return OK even if the plane is not crossing the solid, but the sliced solid is null/nothing, and the this pointer is passed on and cause the exception... &lt;BR /&gt;&lt;BR /&gt;So in your case, if the line does not cross the solid, it throw this exception. Sadly&lt;BR /&gt;&lt;BR /&gt;To fix it, put a try/catch around the Solid3D.Slice call, it this InvalidOperationException happens, then you know the solid and the plane (formed by the line points) are not crossing. &lt;BR /&gt;&lt;BR /&gt;Looking at your code, you have no check around this, but you need to. Make sure the LINE and SOLID are crossing. Note that when you slice, you have no testing if the next line will cross the original solid (now sliced) or new piece...</description>
      <pubDate>Thu, 21 May 2015 12:20:58 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/help-needed-in-slicing-solids-at-given-intervals/m-p/5646279#M40020</guid>
      <dc:creator>augusto.goncalves</dc:creator>
      <dc:date>2015-05-21T12:20:58Z</dc:date>
    </item>
    <item>
      <title>Re: Help needed in slicing solids at given intervals</title>
      <link>https://forums.autodesk.com/t5/net-forum/help-needed-in-slicing-solids-at-given-intervals/m-p/5648093#M40021</link>
      <description>&lt;P&gt;Hi Augusto!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for your kind help and suggestion on try catch.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Coming to the point,&amp;nbsp;First identified objects crossing the line, then checked whether identified object is Solid, then tried to slice the solid. So in this scenario, no chance of slicing null object. So no exception.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then as you suggested I tried the way committing first transaction. Finally concluded it as below.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have stored and reused the points in very raw manner. Suggest me some better options.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Start Transaction&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;Get Lines from User&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;Collect Points from User Selected Lines&lt;/P&gt;
&lt;P&gt;Commit Transaction&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For Each Item in Collection Points&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;Identify Crossing Objects&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;For Each Object in Crossing Objects&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; If Object is Solid then&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Start Transaction&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Slice Object to Point Collection&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Commit Transaction&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;End if&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; Next&lt;/P&gt;
&lt;P&gt;Next&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Code:&lt;/STRONG&gt;&lt;/P&gt;
&lt;PRE&gt;Dim SlicePoints As New List(Of String)
&amp;lt;CommandMethod("SliceSolidsByLines")&amp;gt;
    Public Sub SliceSolid()
        Dim Doc As Document = Application.DocumentManager.MdiActiveDocument
        Dim DB As Database = Doc.Database
        Dim Ed As Editor = Doc.Editor
        Try
            Using Tr As Transaction = DB.TransactionManager.StartTransaction
                Dim BT As BlockTable = Tr.GetObject(DB.BlockTableId, OpenMode.ForRead)
                Dim BTR As BlockTableRecord = Tr.GetObject(BT(BlockTableRecord.ModelSpace), OpenMode.ForRead)
                Dim SObjPrRes As PromptSelectionResult = Ed.GetSelection
                If SObjPrRes.Status = PromptStatus.OK Then
                    Dim SSet As SelectionSet = SObjPrRes.Value
                    For Each Obj As SelectedObject In SSet
                        Dim Ent As Entity = Tr.GetObject(Obj.ObjectId, OpenMode.ForRead)
                        If Obj.ObjectId.ObjectClass.DxfName = "LINE" Then
                            Dim L As Line : L = Ent
                            Dim PT1 As Point3d = L.StartPoint : Dim PT2 As Point3d = L.EndPoint
                            Dim SStr As String = L.StartPoint.X &amp;amp; "|" &amp;amp; L.StartPoint.Y &amp;amp; "|" &amp;amp; L.StartPoint.Z &amp;amp; "|" &amp;amp; L.EndPoint.X &amp;amp; "|" &amp;amp; L.EndPoint.Y &amp;amp; "|" &amp;amp; L.EndPoint.Z
                            SlicePoints.Add(SStr)
                        End If
                    Next
                End If
                Tr.Commit()
            End Using

            For Each AStr As String In SlicePoints
                Dim PTS() As String = AStr.Split("|")
                Dim PT1 As Point3d = New Point3d(PTS(0), PTS(1), PTS(2))
                Dim PT2 As Point3d = New Point3d(PTS(3), PTS(4), PTS(5))
                Dim CrossSelection As SelectionSet = SelectObjectsByCrossingWindow(PT1, PT2)
                For Each Obj2 As SelectedObject In CrossSelection
                    If Obj2.ObjectId.ObjectClass.DxfName = "3DSOLID" Then
                        SliceSolidByPoints(Obj2.ObjectId, PT1, PT2)
                    End If
                Next
            Next
        Catch ex As Exception
            MsgBox(ex.ToString, , "Sub - " &amp;amp; System.Reflection.MethodBase.GetCurrentMethod.Name)
            Clipboard.SetText(ex.ToString)
        End Try
    End Sub
&lt;/PRE&gt;
&lt;P&gt;Regards,&lt;/P&gt;
&lt;P&gt;Srikanth.&lt;/P&gt;</description>
      <pubDate>Fri, 22 May 2015 09:15:55 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/help-needed-in-slicing-solids-at-given-intervals/m-p/5648093#M40021</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2015-05-22T09:15:55Z</dc:date>
    </item>
  </channel>
</rss>

