Jump to content

Recommended Posts

Posted

Hi

i got a question how could i create a array that will contain start and end points of a lines

 

tab(0)[pt1 pt2] first line

tab(1)[pt1 pt2] second line

 

should i use something like this it works but the points are all together so its not perfect :P

 

Dim Tab() As Point3d

Posted

There are several methods to store the coordinates associated with points, and the best choice would probably depend on the downstream requirements of the data.

 

Arrays are a valid option, and may even be the most efficient from a computer processing perspective. If the type and quantity of Points is unknown, arrays are a bit of a handful for the coder, however.

 

The Autodesk.AutoCAD.Geometry.Point3dCollection is very easy to use - as points can be added and subtracted at will.

 

With regard to collections (and their general ease of use), the System.Collections.Generic Namespace offers several other ways of storing points (or any data for that matter) and have the added benefit of making code more universal, i.e., not limited to just AutoCAD routines.

Posted

I think that is no need to create a new thread so i'll ask here have you got any idea why the ray in the function is not visible in the drawing if i use that code without function it works OK

 

    <CommandMethod("src")> _
  Public Sub src()
       Dim lCmd As Editor = Application.DocumentManager.MdiActiveDocument.Editor
       Dim acBaza As Database = lCmd.Document.Database
       Dim trn As Transaction = acBaza.TransactionManager.StartTransaction

       Dim lnL1 As Line = New Line()

       Dim usrPtOp As PromptPointOptions = New PromptPointOptions("Wskarz srodek pomieszczenia :")
       Dim usrPt As PromptPointResult = lCmd.GetPoint(usrPtOp)

       Dim prevPt As Point3d = usrPt.Value
       Dim nextPt As Point3d = usrPt.Value

       If usrPt.Status = PromptStatus.OK Then
           Dim usrPtXmod As Point3d = New Point3d(usrPt.Value.X + 1, usrPt.Value.Y, usrPt.Value.Z)
           Dim promienSledzacy As Ray = New Ray()
           promienSledzacy.BasePoint = usrPt.Value
           promienSledzacy.SecondPoint = usrPtXmod

           'FILTRACJA LINII
           Dim typeValue() As TypedValue = {New TypedValue(0, "line")}
           Dim selFilter As SelectionFilter = New SelectionFilter(typeValue)
           Dim selResult As PromptSelectionResult = lCmd.SelectAll(selFilter)
           Dim ssLinie As SelectionSet = selResult.Value
           Dim tabID() As ObjectId = ssLinie.GetObjectIds
           Dim lnNajblizsza As Line = Nothing
           'TRANSAKCJA
           przecieciaPromienia(promienSledzacy, tabID, prevPt, nextPt, lnNajblizsza)

           'Sprawdzenie ktury punkt jest nizej
           Dim START As Point3d
           Dim FIN As Point3d
           If lnNajblizsza.StartPoint.Y > lnNajblizsza.EndPoint.Y Then
               START = lnNajblizsza.EndPoint
           Else
               START = lnNajblizsza.StartPoint
           End If
       End If
   End Sub

   Public Sub przecieciaPromienia(ByRef promienSledzacy As Ray, ByRef tabID() As ObjectId, ByRef prevPt As Point3d, ByRef nextPt As Point3d, ByRef lnNajblizsza As Line)
       Dim lCmd As Editor = Application.DocumentManager.MdiActiveDocument.Editor
       Dim acBaza As Database = lCmd.Document.Database
       Dim trn As Transaction = acBaza.TransactionManager.StartTransaction
       Try
           Dim btr As BlockTableRecord = trn.GetObject(acBaza.CurrentSpaceId, OpenMode.ForWrite)
           Dim objID As ObjectId
           Dim ra3d As Ray3d = New Ray3d(promienSledzacy.StartPoint, promienSledzacy.SecondPoint)
           Dim odl As Single = 0.0
           Dim odlTmp As Single = 0.0
           For Each objID In tabID
               Dim ln As Line = CType(trn.GetObject(objID, OpenMode.ForRead), Line)
               Dim ls As LineSegment3d = New LineSegment3d(ln.StartPoint, ln.EndPoint)
               Dim ptArray() As Point3d = ls.IntersectWith(ra3d)
               If ptArray Is Nothing Then Continue For
               Dim ptkPrzeciecia As Point3dCollection = New Point3dCollection(ptArray)
               'SZUKANIE PIERWSZEJ NAJBLIZSZEJ LINII   
               '   odl=sqrt ((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)) 

               odlTmp = Math.Sqrt((prevPt.X - ptkPrzeciecia.Item(0).X) * (prevPt.X - ptkPrzeciecia.Item(0).X) + (prevPt.Y - ptkPrzeciecia.Item(0).Y) * (prevPt.Y - ptkPrzeciecia.Item(0).Y))

               If odl = 0.0 Then
                   odl = odlTmp
                   nextPt = ptkPrzeciecia.Item(0)
                   lnNajblizsza = ln
               ElseIf odl >= odlTmp Then
                   nextPt = ptkPrzeciecia.Item(0)
                   lnNajblizsza = ln
               End If
           Next
           lnNajblizsza.Highlight()
           promienSledzacy.SetDatabaseDefaults()
           btr.AppendEntity(promienSledzacy)
           trn.AddNewlyCreatedDBObject(promienSledzacy, True)
           trn.Commit()
       Catch ex As Exception
       Finally
           trn.Dispose()
       End Try
   End Sub

Posted

I wouldn’t exactly say it is a definite but passing open database object references between sub/functions may not be the best practice. I modified the structure to illustrate an alternative. Note: Autodesk.AutoCAD.Geometry objects are not database residents

 

I think the reason that the Ray was not showing was because the top level transaction had not been committed. Again, I modified the routine to illustrate that point.

 

 

    <CommandMethod("src")> _
  Public Sub src()
       Dim lCmd As Editor = Application.DocumentManager.MdiActiveDocument.Editor
       Dim acBaza As Database = lCmd.Document.Database
       Using trn As Transaction = acBaza.TransactionManager.StartTransaction

           Dim lnL1 As Line = New Line()

           Dim usrPtOp As PromptPointOptions = New PromptPointOptions("Wskarz srodek pomieszczenia :")
           Dim usrPt As PromptPointResult = lCmd.GetPoint(usrPtOp)

           Dim prevPt As Point3d = usrPt.Value
           Dim nextPt As Point3d = usrPt.Value

           If usrPt.Status = PromptStatus.OK Then
               Dim usrPtXmod As Point3d = New Point3d(usrPt.Value.X + 1, usrPt.Value.Y, usrPt.Value.Z)
               Dim ls3d As LineSegment3d = New LineSegment3d(usrPt.Value, usrPtXmod)


               'FILTRACJA LINII
               Dim typeValue() As TypedValue = {New TypedValue(0, "line")}
               Dim selFilter As SelectionFilter = New SelectionFilter(typeValue)
               Dim selResult As PromptSelectionResult = lCmd.SelectAll(selFilter)
               Dim ssLinie As SelectionSet = selResult.Value
               Dim tabID() As ObjectId = ssLinie.GetObjectIds

               Dim oid As ObjectId
               'TRANSAKCJA
               oid = przecieciaPromienia(ls3d, tabID, prevPt, nextPt)
               Dim lnNajblizsza As Line = CType(trn.GetObject(oid, OpenMode.ForRead), Line)
               'Sprawdzenie ktury punkt jest nizej
               Dim START As Point3d
               'Dim FIN As Point3d
               If lnNajblizsza.StartPoint.Y > lnNajblizsza.EndPoint.Y Then
                   START = lnNajblizsza.EndPoint
               Else
                   START = lnNajblizsza.StartPoint
               End If

           End If
           trn.Commit()
       End Using
   End Sub

   Public Function przecieciaPromienia(ByRef ls3d As LineSegment3d, ByRef tabID() As ObjectId, ByRef prevPt As Point3d, ByRef nextPt As Point3d) As ObjectId
       Dim lCmd As Editor = Application.DocumentManager.MdiActiveDocument.Editor
       Dim acBaza As Database = Application.DocumentManager.MdiActiveDocument.Database
       Dim lnNajblizsza As Line = Nothing
       Using trn As Transaction = acBaza.TransactionManager.StartTransaction

           Try
               Dim btr As BlockTableRecord = CType(trn.GetObject(acBaza.CurrentSpaceId, OpenMode.ForWrite), BlockTableRecord)
               Dim objID As ObjectId
               Dim ra3d As Ray3d = New Ray3d(ls3d.StartPoint, ls3d.EndPoint)

               Dim odl As Single = 0.0
               Dim odlTmp As Single = 0.0
               For Each objID In tabID
                   Dim ln As Line = CType(trn.GetObject(objID, OpenMode.ForRead), Line)
                   Dim ls As LineSegment3d = New LineSegment3d(ln.StartPoint, ln.EndPoint)
                   Dim ptArray() As Point3d = ls.IntersectWith(ra3d)
                   If ptArray Is Nothing Then Continue For
                   Dim ptkPrzeciecia As Point3dCollection = New Point3dCollection(ptArray)
                   'SZUKANIE PIERWSZEJ NAJBLIZSZEJ LINII   
                   '   odl=sqrt ((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)) 

                   odlTmp = Math.Sqrt((prevPt.X - ptkPrzeciecia.Item(0).X) * (prevPt.X - ptkPrzeciecia.Item(0).X) + (prevPt.Y - ptkPrzeciecia.Item(0).Y) * (prevPt.Y - ptkPrzeciecia.Item(0).Y))

                   If odl = 0.0 Then
                       odl = odlTmp
                       nextPt = ptkPrzeciecia.Item(0)

                   ElseIf odl >= odlTmp Then
                       nextPt = ptkPrzeciecia.Item(0)

                   End If
                   lnNajblizsza = ln
               Next
               lnNajblizsza.Highlight()
               Dim promienSledzacy As Ray = New Ray()
               promienSledzacy.BasePoint = ls3d.StartPoint
               promienSledzacy.SecondPoint = ls3d.EndPoint
               promienSledzacy.SetDatabaseDefaults()
               btr.AppendEntity(promienSledzacy)
               trn.AddNewlyCreatedDBObject(promienSledzacy, True)
               trn.Commit()
           Catch ex As Exception
           End Try
           Return lnNajblizsza.ObjectId
       End Using
   End Function

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...