Create layers from a comma deliminated file

Create layers from a comma deliminated file

richie_hodgson
Collaborator Collaborator
1,216 Views
6 Replies
Message 1 of 7

Create layers from a comma deliminated file

richie_hodgson
Collaborator
Collaborator

Hi, I have been hunting for the best method to read a comma deliminated file with Layername,linetype,colour format and then create that layer if it doesn't exist, I have so far been unable to track down an understandable method. Any help welcolme. I have a similar lisp file that replaces missing layers that have been purged by accident, rather than copying and pasting onto an unpurged template.

Richie
0 Likes
Accepted solutions (2)
1,217 Views
6 Replies
Replies (6)
Message 2 of 7

R.Gerritsen4967
Advocate
Advocate

I've made a similar function here at the office.

It reads layers, dimstyle and textstyles from an XML-file.

 

I found XML files a bit more versatile than comma delimited files.

 

Whenever I create a new drawing, start working on a very old drawing or work on a drawing from outside our office, I start the function.

 

Some old layernames are renamed/merged to new layernames. And our standard dimstyle and textstyles are set up. (And also some other drawing parameters)

 

The bit that makes the layers works like this:

I read the layer specifics (name, color, linetype and some others) from the XML file, and then check if this layer already exists in the layertable.

If not, I create the layer. If it does exist, I set the color and linetype from the XML file just to be sure it ends up the way I want it.

In the XML I also have a name for the equivalent of the new layer. So if there is already a layer in the drawing with this equivalent name, I rename it to the new name. Offcourse If somehow the new layer already exists, I just merge them.

 

I don't have a lot of experience with coding for AutoCAD, but I made this function almost completely with the help of the AutoCAD .NET Developer's Guide.

 

I won't post all my code here, but if you can be more precise as to where you got stuck it might be easier to point you in the right direction.

 

 

0 Likes
Message 3 of 7

Anonymous
Not applicable
Accepted solution

Hi Richie,

 

Quickly compiled the code from my library, portions of code from internet. Not checked.

Give a try - may be of any help.

 

<CommandMethod("CreateLayersFromTextFile")> Public Sub CreateLayersFromTextFile()

        Dim Cnt As Integer

        Dim Doc As Document = Application.DocumentManager.MdiActiveDocument
        Dim DB As Database = Application.DocumentManager.MdiActiveDocument.Database
        Dim ED As Editor = Doc.Editor

        Try
            Dim FNM As String = "C:\Text.txt"
            Dim LyrDef As String
            If System.IO.File.Exists(FNM) Then
                Dim ObjReader As New System.IO.StreamReader(FNM)
                Do While ObjReader.Peek <> -1
                    LyrDef = ObjReader.ReadLine
                    Dim LstLyrDef() As String = LyrDef.Split(",")
                    If LstLyrDef(0) <> "" And LstLyrDef(1) = "" And LstLyrDef(2) = "" And LstLyrDef(3) = "" Then
                        If CreateLayer(LstLyrDef(0), LstLyrDef(1), LstLyrDef(2), LstLyrDef(3)) = True Then ' Not Checked for Errors fullu 
                            Cnt = Cnt + 1
                            ED.WriteMessage(vbLf & " Successfully Created Layer: " & LstLyrDef(0))
                        End If
                    End If
                Loop

            Else
                Application.ShowAlertDialog("File Not Exist - " & FNM)
            End If

            ED.WriteMessage(vbLf + "Process Completed - Created " + Cnt + " Layers.")
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
    Function CreateLayer(ByVal LyrName As String, ByVal LyrColor As String, ByVal LineType As String, ByVal LineWeight As String) As Boolean
        Try
            Dim Doc As Document = Application.DocumentManager.MdiActiveDocument
            Dim DB As Database = Doc.Database
            Using Tr As Transaction = DB.TransactionManager.StartTransaction
                Dim LyrTable As LayerTable = Tr.GetObject(DB.LayerTableId, OpenMode.ForWrite)
                If LyrTable.Has(LyrName) Then
                Else
                    Dim LyrTblRecord As LayerTableRecord = New LayerTableRecord
                    LyrTblRecord.Name = LyrName
                    LyrTblRecord.Color = Color.FromColorIndex(ColorMethod.ByAci, CInt(LyrColor))
                    Dim LTT As LinetypeTable
                    LTT = Tr.GetObject(DB.LinetypeTableId, OpenMode.ForRead)
                    If LTT.Has(LineType) Then 'Checking for Linetype availability
                        LyrTblRecord.LinetypeObjectId = LTT(LineType)
                    Else
                        Try ' Loading Linetype from Linetype Definition File
                            Dim LTFilePath As String = "F:\VB.Net\ACAD\Mine.lin" ' Change the file name as applicable
                            If LoadLineType(LTFilePath, LineType) Then MsgBox("Loaded Line Type - " & LineType)
                            LyrTblRecord.LinetypeObjectId = LTT(LineType)
                        Catch ex As Exception
                            MsgBox(ex.ToString)
                        End Try

                    End If
                    Select Case CDbl(LineWeight) ' I couldn't find the otherway making it simple. suggestions please
                        Case Is = 0.0
                            LyrTblRecord.LineWeight = Autodesk.AutoCAD.DatabaseServices.LineWeight.LineWeight000
                        Case Is = 0.05
                            LyrTblRecord.LineWeight = Autodesk.AutoCAD.DatabaseServices.LineWeight.LineWeight005
                        Case Is = 0.09
                            LyrTblRecord.LineWeight = Autodesk.AutoCAD.DatabaseServices.LineWeight.LineWeight009
                        Case Is = 0.13
                            LyrTblRecord.LineWeight = Autodesk.AutoCAD.DatabaseServices.LineWeight.LineWeight013
                        Case Is = 0.15
                            LyrTblRecord.LineWeight = Autodesk.AutoCAD.DatabaseServices.LineWeight.LineWeight015
                        Case Is = 0.18
                            LyrTblRecord.LineWeight = Autodesk.AutoCAD.DatabaseServices.LineWeight.LineWeight018
                        Case Is = 0.2
                            LyrTblRecord.LineWeight = Autodesk.AutoCAD.DatabaseServices.LineWeight.LineWeight020
                        Case Is = 0.25
                            LyrTblRecord.LineWeight = Autodesk.AutoCAD.DatabaseServices.LineWeight.LineWeight025
                        Case Is = 0.3
                            LyrTblRecord.LineWeight = Autodesk.AutoCAD.DatabaseServices.LineWeight.LineWeight030
                        Case Is = 0.35
                            LyrTblRecord.LineWeight = Autodesk.AutoCAD.DatabaseServices.LineWeight.LineWeight035
                        Case Is = 0.4
                            LyrTblRecord.LineWeight = Autodesk.AutoCAD.DatabaseServices.LineWeight.LineWeight040
                        Case Is = 0.5
                            LyrTblRecord.LineWeight = Autodesk.AutoCAD.DatabaseServices.LineWeight.LineWeight050
                        Case Is = 0.7
                            LyrTblRecord.LineWeight = Autodesk.AutoCAD.DatabaseServices.LineWeight.LineWeight070
                        Case Is = 0.8
                            LyrTblRecord.LineWeight = Autodesk.AutoCAD.DatabaseServices.LineWeight.LineWeight080
                        Case Is = 0.9
                            LyrTblRecord.LineWeight = Autodesk.AutoCAD.DatabaseServices.LineWeight.LineWeight090
                        Case Is = 1.0
                            LyrTblRecord.LineWeight = Autodesk.AutoCAD.DatabaseServices.LineWeight.LineWeight100
                        Case Is = 1.06
                            LyrTblRecord.LineWeight = Autodesk.AutoCAD.DatabaseServices.LineWeight.LineWeight106
                        Case Is = 1.2
                            LyrTblRecord.LineWeight = Autodesk.AutoCAD.DatabaseServices.LineWeight.LineWeight120
                        Case Is = 1.4
                            LyrTblRecord.LineWeight = Autodesk.AutoCAD.DatabaseServices.LineWeight.LineWeight140
                        Case Is = 1.58
                            LyrTblRecord.LineWeight = Autodesk.AutoCAD.DatabaseServices.LineWeight.LineWeight158
                        Case Is = 2.0
                            LyrTblRecord.LineWeight = Autodesk.AutoCAD.DatabaseServices.LineWeight.LineWeight200
                        Case Is = 2.11
                            LyrTblRecord.LineWeight = Autodesk.AutoCAD.DatabaseServices.LineWeight.LineWeight211
                    End Select

                    'LyrTblRecord.LineWeight = LineWeight
                    'LyrTblRecord.UpgradeOpen()
                    Dim LyrObjId As ObjectId = LyrTable.Add(LyrTblRecord)
                    Tr.AddNewlyCreatedDBObject(LyrTblRecord, True)
                    Tr.Commit()
                End If

            End Using
            Return True
        Catch ex As System.Exception
            MsgBox(ex.ToString)
            Return False
        End Try
    End Function

    Function LoadLineType(ByVal FLName As String, ByVal LTName As String) As Boolean
        Try
            Dim DB As Database = Application.DocumentManager.MdiActiveDocument.Database
            Dim TR As Transaction = DB.TransactionManager.StartTransaction()
            Using TR
                Dim BT As BlockTable = TR.GetObject(DB.BlockTableId, OpenMode.ForRead)
                Dim MS As BlockTableRecord = TR.GetObject(BT(BlockTableRecord.ModelSpace), OpenMode.ForWrite, True)
                Dim LTypTbl As LinetypeTable = TR.GetObject(DB.LinetypeTableId, OpenMode.ForWrite)
                If LTypTbl.Has(LTName) = False Then
                    DB.LoadLineTypeFile(LTName, FLName)
                End If
                If LTypTbl.Has(LTName) = True Then
                    TR.Commit() : Return True
                Else
                    TR.Commit() : Return False
                End If
            End Using
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Function

Regards,

Srikanth.

0 Likes
Message 4 of 7

richie_hodgson
Collaborator
Collaborator

Hi

 

Yes eventually I would like to turn it into a drawing setup routine, but for the moment all I need is the best method for stepping through a .txt or .csv file and splitting each line into its three components. I want the .csv, .txt file to be easily edited at this stage but locked when I am happy so no one can fiddle with it. As before the file will be a list of layername, linetype, colour. I am fairly certain I can convert the result of each line to a new layer if it doesn't exist myself. I am just getting into c# programming so working out how it all works. I will do a bit more hunting this lunchtime in case I can find something that makes sense.

 

Richie

Richie
0 Likes
Message 5 of 7

richie_hodgson
Collaborator
Collaborator

Hi

 

Thanks for that, it was a bit more than I expected. I would like to remove the lineweight bit of the code for the moment since we use the CTB file to drive pen thinknesses so I assume I just need to remove 

 

Select Case CDbl(LineWeight) ' I couldn't find the otherway making it simple. suggestions please

...
'LyrTblRecord.LineWeight = LineWeight

 

Or do I need to do something else.

 

Richie

 

Richie
0 Likes
Message 6 of 7

Anonymous
Not applicable
Accepted solution

Hi Richie,

 

I think, you find it useful, but don't require Lineweight. If you feel, I am in line with your requirement, you may avoid Lineweight from the function by updating the code as shown below.

 

  • In Public Sub CreateLayersFromTextFile(), update the code as below

 1.png

  • Remove ByVal LineWeight As String from CreateLayer Function parameters.

2.png

  • Comment out code from Select Case CDbl(LineWeight) to End Select / remove it.

3.png

  • Regarding Lineweight from my Code – since I couldn’t pass the string/double value read from text file to LyrTblRecord.LineWeight directly, I had to use to Select Case. As I felt it is very raw method of using code, requested for suggestions to achieve the same with any advanced functions, if available. But nothing to do with the code, if not found any better ideas in coding.

     

Regards,

Srikanth.

0 Likes
Message 7 of 7

richie_hodgson
Collaborator
Collaborator

You are a star, will do the necessary fiddling.Cat LOL Just getting my IT section to create a virtual environment for coding so I dont break anything important. Will hopefully be able do some testing before the end of the month.

Richie