Author Topic: Import Polyline to Autocad speed  (Read 2075 times)

0 Members and 1 Guest are viewing this topic.

sigster

  • Newt
  • Posts: 31
Import Polyline to Autocad speed
« on: January 30, 2018, 05:30:50 PM »
Hi


HI am play with dotspatial to import SHP file to Autopcad if I import one file it is ok but a few the speed is little show
is someone who can help how I can speed this up



Code: [Select]
       <CommandMethod("XYinsert")> _
        Public Sub XYinsert()
            ReadSHPfile("C:\map\streetline.shp", "st")
        End Sub

a few

        Public Sub ReadSHPfile(ByVal slod, ByVal Layerinst)

            Dim doc As Document = Application.DocumentManager.MdiActiveDocument
            Dim db As Database = doc.Database
            Dim ed As Editor = doc.Editor

            Using acTrans As Transaction = db.TransactionManager.StartTransaction()

                Dim acBlkTbl As BlockTable
                acBlkTbl = CType(acTrans.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable)

                Dim acBlkTblRec As BlockTableRecord
                acBlkTblRec = CType(acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), OpenMode.ForWrite), BlockTableRecord)

                Dim shapeActual As FeatureSet = FeatureSet.Open(slod)

                shapeActual.FillAttributes()
                Dim dt As System.Data.DataTable = shapeActual.DataTable

                Dim pline As Polyline = New Polyline()
                pline.SetDatabaseDefaults()
                Dim ptColl As Point3dCollection = New Point3dCollection()

                For count As Integer = 0 To shapeActual.Features.Count - 1
                    For Each feature As IFeature In shapeActual.Features 'circle through the features
                        For i As Integer = 0 To feature.BasicGeometry.NumGeometries - 1 'circle through the parts of one feature
                            For Each coord As Coordinate In feature.BasicGeometry.GetBasicGeometryN(i).Coordinates() 'circle through the coordinates of the feature part
                                'Dim AdresX = coord.X
                                'Dim AdresY = coord.Y

                                pline.AddVertexAt(count, New Point2d(coord.X, (coord.Y)), 0.0, 0.0, 0.0)
                                count = count + 1

                                Dim pt As New Point3d(coord.X, coord.Y, coord.Z)
                                pts.Add(pt)

                            Next
                            '    installa1()
                            ed.WriteMessage(vbCr & feature.FeatureType.ToString)
                        Next

                        insert_with_layer(Layerinst)
                        pts.Clear()

                    Next

                Next
                acTrans.Commit()
            End Using

        End Sub


        Public Sub insert_with_layer(ByVal Layer_name As String)

            Dim doc As Document = Application.DocumentManager.MdiActiveDocument
            Dim db As Database = doc.Database
            Dim trans As Transaction
            Dim transMan As Autodesk.AutoCAD.ApplicationServices.TransactionManager
            transMan = db.TransactionManager
            trans = transMan.StartTransaction

            ' Dim lne As New Line(pt1, pt2)
            Dim pline As Polyline = New Polyline()
            Dim bt As BlockTable
            Dim btr As BlockTableRecord

            bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead)
            btr = trans.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite)

            Dim i As Integer = 0
            For Each pnt As Point3d In pts
                Dim pt As New Point3d(pnt.X, pnt.Y, pnt.Z)

                pline.AddVertexAt(i, New Point2d(pt.X, (pt.Y)), 0.0, 0.0, 0.0)
                i += 1
            Next

            pline.Layer = Layer_name
            btr.AppendEntity(pline)

            trans.AddNewlyCreatedDBObject(pline, True)
            trans.Commit()
            trans.Dispose()
            transMan.Dispose()

        End Sub





MickD

  • King Gator
  • Posts: 3636
  • (x-in)->[process]->(y-out) ... simples!
Re: Import Polyline to Autocad speed
« Reply #1 on: January 30, 2018, 05:47:29 PM »
after just a quick look:

You probably don't need to create 'new' polyline or collection objects, just reset/clear them, this will save allocating new memory every iteration. Same for any other objects you can probably reuse.

It sounds counter intuitive but I've found that having a separate transaction for each entity is sometimes quicker than trying to do all in the one loop then the commit. You can wrap the loop in one transaction then add the entities with an inner transaction, something like a helper function 'postToDatabase'.
Like I said, sounds like more overhead but that has been my experience at times :)
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

jmaeding

  • Bull Frog
  • Posts: 304
  • I'm just here for the Shelties.
Re: Import Polyline to Autocad speed
« Reply #2 on: January 31, 2018, 11:54:49 AM »
You did pretty well in terms of the acad part.
I would guess its the dotspacial parts that are slow.
When you run in debug, watch the number that shows to the right on a line of code stepped through.
It will say like 10ms, meaning the last operation took 10ms.
Using that, you can quickly find the slower parts, and also test in your own environment.

My typical advice is put all in one transaction, which MickD says can hurt and I will have to remember that.
In speed cases, you just try both as transaction grouping is easy.

I also use dotspacial and was actually getting ready to do a shapefile->google earth tool as I already did my own acad->kmz that is about 100x better than the civil3d one. I use the mapguide api for coord translations btw. The advantage is the coord system "short" names match what acad map uses as map uses the mapguide engine also. You can use it in plain acad or bcad, no need for verticals.
James Maeding