Author Topic: generating linetype results in error  (Read 5847 times)

0 Members and 1 Guest are viewing this topic.

ROYNIJKAMP

  • Mosquito
  • Posts: 12
generating linetype results in error
« on: November 17, 2015, 11:12:10 AM »
For a project we receive a bunch of DXF files with an XML files with additional info of the contents in the DXF file.
Every week we receive a new set of DXF files and XML files, wich have to be converted to DWG.

To automate this repetitive task i created an function with VB.NET.
This function works as follow:
Function InsertDXF()
   for each xmlFile in folder
      - read xml file to a datatable
      - create layer based on info from datatable function InsertLayer()
      - insert DXF
    next xmlFile
   - sub buildLegend() //create an summary of layer and lenght of all objects on layer
End function
the above function works very well when i do not make a custom linetype in the function InsertLayer()
When i add the code for my custom linetype i get an error INTERNAL ERROR: !dbobji.cpp@8615: eNotOpenForWrite
The layer and linetype are generated perfect, but the function buildLegend() ends with an error.
The error message in the dmpuserinfo.xml points to acBlkTblRec.AppendEntity(acLine) in the function buildLegend()
To me it looks like something goes wrong on the linetypetable, but i have no idea how to fix this.

Could some one give me advise?
Is there something wrong with the linetype creation method?

Below the function for creating the layer and linetype, the sub for creating the summary and some dumpdata.

Code - vb.net: [Select]
  1. Public Shared Function InsertLayer(ByRef doc As Document, ByRef ed As Editor, ByRef db As Autodesk.AutoCAD.DatabaseServices.Database, ByRef dtKlicLayerPrefList As System.Data.DataTable, strLayName As String, sLayerBase As String)
  2.         Using acLockDoc As DocumentLock = doc.LockDocument()
  3.                 Using acTrans As Autodesk.AutoCAD.DatabaseServices.Transaction = db.TransactionManager.StartTransaction
  4.                         Try
  5.                                 Dim ltTable As Autodesk.AutoCAD.DatabaseServices.LayerTable = acTrans.GetObject(db.LayerTableId, Autodesk.AutoCAD.DatabaseServices.OpenMode.ForRead)
  6.                                 If ltTable.Has(strLayName) Then
  7.                                         'layer exists
  8.                                         'TODO: check layer settings
  9.                                 Else
  10.                                         'create layer
  11.                                         Dim ltTableR As Autodesk.AutoCAD.DatabaseServices.LayerTableRecord = New Autodesk.AutoCAD.DatabaseServices.LayerTableRecord()
  12.                                         ltTableR.Name = strLayName
  13.                                         ltTable.UpgradeOpen()
  14.                                         ltTable.Add(ltTableR)
  15.                                         Try
  16.                                                 Dim arrLayerBase() As String = sLayerBase.Split("_")
  17.                                                 Dim expression As String = "[type] = '" & arrLayerBase(0) & "'"
  18.                                                 Dim drResults() As System.Data.DataRow = dtKlicLayerPrefList.Select(expression)
  19.                                                 If drResults.Length > 0 Then
  20.                                                         For Each row As DataRow In drResults
  21.                                                                 ltTableR.Color = Autodesk.AutoCAD.Colors.Color.FromColorIndex(ColorMethod.ByAci, CShort(row(1).ToString))
  22.                                                                 Dim oLineWeight As Autodesk.AutoCAD.DatabaseServices.LineWeight
  23.                                                                 Dim sLineWeight As String = row(4).ToString
  24.                                                                 If sLineWeight.Length > 0 Then
  25.                                                                         oLineWeight = CInt(sLineWeight.Substring(sLineWeight.Length - 3))
  26.                                                                         ltTableR.LineWeight = oLineWeight
  27.                                                                 End If
  28.                                                                 ltTableR.Description = arrLayerBase(1) & " " & row(6).ToString
  29.                                                                 Dim sLineType As String = row(2)
  30.                                                                 Dim sLinetTypeSuff As String = row(3)
  31.                                                                 Dim acLineTypeTbl As LinetypeTable = acTrans.GetObject(db.LinetypeTableId, OpenMode.ForWrite)
  32.                                                                 If acLineTypeTbl.Has(sLineType) = False Then
  33.                                                                         'create custom linetype bases on info from DB
  34.                                                                         Dim tt As TextStyleTable = acTrans.GetObject(db.TextStyleTableId, OpenMode.ForRead)
  35.                                                                         Dim ltr As LinetypeTableRecord = New LinetypeTableRecord()
  36.                                                                         ltr.Name = "ITM " & sLineType
  37.                                                                         ltr.AsciiDescription = " ---- " & sLineType & " ---- "
  38.                                                                         ltr.PatternLength = 3.0
  39.                                                                         ltr.NumDashes = 3
  40.                                                                         'dash #1
  41.                                                                         ltr.SetDashLengthAt(0, 4)
  42.                                                                         'dash #2
  43.                                                                         ltr.SetDashLengthAt(1, -4)
  44.                                                                         ltr.SetShapeStyleAt(1, tt("Standard"))
  45.                                                                         ltr.SetShapeNumberAt(1, 0)
  46.                                                                         'text size
  47.                                                                         Dim iLtTXTscale As Integer = 1
  48.                                                                         Dim iLtTXTYpos As Double = ((iLtTXTscale / 2) * -1)
  49.                                                                         ltr.SetShapeOffsetAt(1, New Vector2d(-0.8, iLtTXTYpos))
  50.                                                                         ltr.SetShapeScaleAt(1, iLtTXTscale)
  51.                                                                         ltr.SetShapeIsUcsOrientedAt(1, False)
  52.                                                                         ltr.SetShapeRotationAt(1, 0)
  53.                                                                         ltr.SetTextAt(1, sLineType)
  54.                                                                         'dash #3
  55.                                                                         Dim iLtDashSize As Integer = (sLineType.Length * iLtTXTscale) * -1
  56.                                                                         ltr.SetDashLengthAt(2, iLtDashSize)
  57.                                                                         Dim ltId As ObjectId = acLineTypeTbl.Add(ltr)
  58.                                                                         acTrans.AddNewlyCreatedDBObject(ltr, True)
  59.                                                                         ltTableR.LinetypeObjectId = ltId
  60.                                                                 End If
  61.                                                         Next
  62.                                                 End If
  63.                                         Catch ex As Autodesk.AutoCAD.Runtime.Exception
  64.                                                 MsgBox("Error creating linetype" & vbcrlf 7 ex.Message.ToString)
  65.                                         End Try
  66.                                         acTrans.AddNewlyCreatedDBObject(ltTableR, True)
  67.                                 End If
  68.                                 acTrans.Commit()
  69.                         Catch ex As Autodesk.AutoCAD.Runtime.Exception
  70.                                 MsgBox("Error creating layer " & strLayName & vbcrlf & ex.Message.ToString)
  71.                         End Try
  72.                 End Using
  73.         End Using
  74. End Function
  75.  
Code - vb.net: [Select]
  1. Private Sub buildLegend()
  2.         Dim ptInsert As Point3d
  3.         Dim acBlkTbl As BlockTable
  4.         Dim acBlkTblRec As BlockTableRecord
  5.  
  6.         Dim acDoc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
  7.         Dim acCurDb As Database = acDoc.Database
  8.  
  9.         Autodesk.AutoCAD.Internal.Utils.SetFocusToDwgView()
  10.  
  11.         Using acLockDoc As DocumentLock = acDoc.LockDocument()
  12.  
  13.                 Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
  14.                         acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)
  15.  
  16.                         acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
  17.                         Dim pPtRes As PromptPointResult
  18.                         Dim pPtOpts As PromptPointOptions = New PromptPointOptions("")
  19.                         pPtOpts.Message = vbLf & "Pick point! "
  20.                         pPtRes = acDoc.Editor.GetPoint(pPtOpts)
  21.                         ptInsert = pPtRes.Value
  22.  
  23.                         Dim dtSelectedResultsTMP As System.Data.DataTable = New System.Data.DataTable()
  24.                         clsQuantity.createList(dtSelectedResultsTMP) '#### function wich asks for user selection and save data to datatable
  25.                         Dim dtDataView As System.Data.DataView = New System.Data.DataView(dtSelectedResultsTMP)
  26.                         dtDataView.Sort = "qtytype DESC, layer DESC"
  27.                         Dim dtSelectedResults As System.Data.DataTable = dtDataView.ToTable()
  28.                         dtSelectedResultsTMP = Nothing
  29.                         dtDataView = Nothing
  30.  
  31.                         If Not dtSelectedResults.Rows.Count = 0 Then
  32.                                 Dim ptCurrentItem As Point2d = New Point2d(ptInsert.X, ptInsert.Y)
  33.                                 For Each dtFilterRow As System.Data.DataRow In dtSelectedResults.Rows
  34.                                         Dim acMtext As MText = New MText()
  35.                                         Dim strTekst As String
  36.                                         If dtFilterRow.Item(5).ToString.Length = 0 Then
  37.                                                 strTekst = dtFilterRow.Item(0).ToString 'layer
  38.                                         Else
  39.                                                 strTekst = dtFilterRow.Item(5).ToString 'layer and quantity
  40.                                         End If
  41.                                        
  42.                                                 Try
  43.                                                         Dim ptEnd As Point2d
  44.                                                         ptCurrentItem = New Point2d(ptCurrentItem.X, ptCurrentItem.Y + ptSpacing)
  45.                                                         ptEnd = New Point2d(ptCurrentItem.X + ptLength, ptCurrentItem.Y)
  46.                                                         Dim acLine As Polyline = New Polyline()
  47.                                                         acLine.AddVertexAt(0, ptCurrentItem, 0, CDbl(dtFilterRow.Item(2).ToString), CDbl(dtFilterRow.Item(2).ToString))
  48.                                                         acLine.AddVertexAt(1, ptEnd, 0, CDbl(dtFilterRow.Item(2).ToString), CDbl(dtFilterRow.Item(2).ToString))
  49.                                                         acLine.Layer = dtFilterRow.Item(0).ToString
  50.                                                         acLine.Linetype = dtFilterRow.Item(1).ToString
  51.                                                         acLine.LinetypeScale = CDbl(dtFilterRow.Item(6).ToString)
  52.                                                        
  53.                                                         acBlkTblRec.AppendEntity(acLine) '#### This line throws exception
  54.                                                         acTrans.AddNewlyCreatedDBObject(acLine, True)
  55.  
  56.                                                         acMtext.Location = New Point3d(ptCurrentItem.X + (ptLength * 1.1), ptCurrentItem.Y, 0)
  57.  
  58.                                                 Catch ex As Autodesk.AutoCAD.Runtime.Exception
  59.                                                         MsgBox(ex.Message.ToString)
  60.                                                 End Try
  61.                                                
  62.                                         End Select
  63.                                         acMtext.Contents = strTekst
  64.                                         acMtext.TextStyleId = textStyleTbl("arial")
  65.                                         acMtext.TextHeight = entTextHeight
  66.                                         acMtext.Layer = strLayer
  67.                                         acMtext.Attachment = AttachmentPoint.MiddleLeft
  68.                                         acBlkTblRec.AppendEntity(acMtext)
  69.                                         acTrans.AddNewlyCreatedDBObject(acMtext, True)
  70.                                 Next
  71.                         End If
  72.                         acTrans.Commit()
  73.                 End Using
  74.         End Using
  75. End Sub
  76.  
AutoCAD dump data
Code - vb.net: [Select]
  1. Clr Data:
  2.    at AcDbBlockTableRecord.appendAcDbEntity(AcDbBlockTableRecord* , AcDbObjectId* , AcDbEntity* )
  3.  
  4.    at AcDbBlockTableRecord.appendAcDbEntity(AcDbBlockTableRecord* , AcDbObjectId* , AcDbEntity* )
  5.  
  6.    at Autodesk.AutoCAD.DatabaseServices.BlockTableRecord.AppendEntity(Entity entity)
  7.  
  8.    at AnaconToolSet.tabBetaFunctions.Button1_Click(Object sender, EventArgs e)
  9.  
  10.    at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
  11.  
  12.    at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
  13.  
  14.    at System.Windows.Forms.Control.WndProc(Message& m)
  15.  
  16.    at System.Windows.Forms.ButtonBase.WndProc(Message& m)
  17.  
  18.    at System.Windows.Forms.Button.WndProc(Message& m)
  19.  
  20.    at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
  21.  

edit kdub:->  [code = vbnet]
« Last Edit: November 18, 2015, 05:44:12 AM by Kerry »

Bryco

  • Water Moccasin
  • Posts: 1883
Re: generating linetype results in error
« Reply #1 on: November 17, 2015, 08:51:09 PM »
Havent had a good look, but did you try  db.LoadLineTypeFile, rather than directly making the linetype in the dwg

MickD

  • King Gator
  • Posts: 3636
  • (x-in)->[process]->(y-out) ... simples!
Re: generating linetype results in error
« Reply #2 on: November 17, 2015, 09:28:31 PM »
I only had a quick look too but I've come across similar exceptions and more often than not it has to do with 'when' you add new items to the database.
For instance, try moving

acTrans.AddNewlyCreatedDBObject(ltTableR, True)

to just after

ltTable.Add(ltTableR)

and then make any modifications.

You don't have to commit it yet but it has now been added to the db and has therefore got somewhere concrete to live and be worked on.

Just a thought :)
"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

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: generating linetype results in error
« Reply #3 on: November 17, 2015, 11:05:06 PM »
Quote from: MickD
I only had a quick look too but I've come across similar exceptions and more often than not it has to do with 'when' you add new items to the database.
For instance, try moving .....

I've seen that too.
... though I must 'fess up to hardly ever reading VB code anymore ...


As an aside :
doesn't the DXF file carry the Layer/Linetype info with it ??
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

ROYNIJKAMP

  • Mosquito
  • Posts: 12
Re: generating linetype results in error
« Reply #4 on: November 18, 2015, 02:59:12 AM »
....
As an aside :
doesn't the DXF file carry the Layer/Linetype info with it ??
No the DXF file only contains drawing elements on layer 0 with all settings default.
Thats the reason that i create per DXF file a layer with a linetype.

I only had a quick look too but I've come across similar exceptions and more often than not it has to do with 'when' you add new items to the database.
For instance, try moving

acTrans.AddNewlyCreatedDBObject(ltTableR, True)

to just after

ltTable.Add(ltTableR)

and then make any modifications.

You don't have to commit it yet but it has now been added to the db and has therefore got somewhere concrete to live and be worked on.

Just a thought :)
MickD, i tried you're suggestion but with no success.

MickD

  • King Gator
  • Posts: 3636
  • (x-in)->[process]->(y-out) ... simples!
Re: generating linetype results in error
« Reply #5 on: November 18, 2015, 04:50:37 AM »
ok, I don't see a 'Commit()' in that function either(??). If you don't commit the transaction the new DBObject won't be available.
"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

ROYNIJKAMP

  • Mosquito
  • Posts: 12
Re: generating linetype results in error
« Reply #6 on: November 18, 2015, 04:58:39 AM »
ok, I don't see a 'Commit()' in that function either(??). If you don't commit the transaction the new DBObject won't be available.
Oops i removed the commit bij accident while removing not needed comments / todo's in mij code.
I modified my function and added the commit.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: generating linetype results in error
« Reply #7 on: November 18, 2015, 05:11:50 AM »
What does the custom linetype look like.
Can you post it ?

Can you post a minimalised solution that demonstrates the problem ?
... and a DXF / XML file if needed ?

Which AutoCAD build are you programming to ?
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: generating linetype results in error
« Reply #8 on: November 19, 2015, 02:25:10 AM »

Where are you calling InsertLayer()  from ??

and when ?


What is the contents of the dtKlicLayerPrefList DataTable ?


kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

ROYNIJKAMP

  • Mosquito
  • Posts: 12
Re: generating linetype results in error
« Reply #9 on: November 19, 2015, 04:09:02 AM »
What does the custom linetype look like.
Can you post it ?
This is how the linetype looks, only the tekst inside is different per layer.

Quote
Can you post a minimalised solution that demonstrates the problem ?
I don't have a minimalised solution at this moment
Quote
Which AutoCAD build are you programming to ?
AutoCAD Civil 3D 2016.
Where are you calling InsertLayer()  from ??
and when ?
As mentioned in my openingpost the function gets called from a function InsertDXF() which itterates through a directory with XML and DXF files.
This function works fine when i skip the linetype generation.
Quote
What is the contents of the dtKlicLayerPrefList DataTable ?
The contents are:
- layername
- layercolor
- lineweight
- plotstyle
- linetypetekst


The function is working fine for the past couple of months without the linetype creation.
Now i got the question te have custom linetypes, so i added the linetyp creation but this breaks something in the table.

MickD

  • King Gator
  • Posts: 3636
  • (x-in)->[process]->(y-out) ... simples!
Re: generating linetype results in error
« Reply #10 on: November 19, 2015, 03:09:37 PM »
Just a thought..

Are you creating the linetype inside a transaction along with the other code that uses it? If so perhaps you could try creating the linetype in its own separate transaction then start a new one to perform the other work that uses the new linetype? This way the new object should be well and truly in the db and available.

eg:
instead of -
start trans
    createLinType()
    doStuff()
end trans

Try -
start trans1
    createLinType()
end trans1

start trans2
    doStuff()
end trans2
"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

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: generating linetype results in error
« Reply #11 on: November 19, 2015, 08:02:22 PM »
Hi Mick,
That was my thought as well.

I was going to suggest a stand alone creator for Linetypes
Ditto for Layers.

Single responsibility principle makes resolving issues a LOT simpler.

I had intended attempting to hack some code together, but spare time is sparse at the moment.

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

MickD

  • King Gator
  • Posts: 3636
  • (x-in)->[process]->(y-out) ... simples!
Re: generating linetype results in error
« Reply #12 on: November 19, 2015, 08:42:42 PM »
...

Single responsibility principle makes resolving issues a LOT simpler.

...

Definitely!
I used to pass transactions around a lot with the thinking it would be more efficient. When I refactored my code to be more like 'Single Responsibility' I actually found that it ran quicker :)
"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

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: generating linetype results in error
« Reply #13 on: November 22, 2015, 11:41:49 PM »
How about this ??

I believe you should make the linetypes in a separate methos and transaction to the rest of the stuff you're doing.
The values you were passing as properties to the LinetypeTableRecord were incorrect as far as I could tell.

I'll post some code later and try to code it so that it is relatively self explanatory ...
In short,
dash[0] is the line
dash[1] is half the gap between lines
   and also contains the text information with the offset from the end of dash[1]
dash[2] is half the gap between lines
patternLength  is the sum of the dash lengths.

Regards,
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: generating linetype results in error
« Reply #14 on: November 23, 2015, 12:01:30 AM »
Have a play.
Source is attached at the bottom of the post.

Code - C#: [Select]
  1. // Proof of concept code
  2. // codehimbelonga kdub@theSwamp 2015.11.23
  3.  
  4. using Autodesk.AutoCAD.Runtime;
  5. using Autodesk.AutoCAD.ApplicationServices;
  6. using Autodesk.AutoCAD.DatabaseServices;
  7. using Autodesk.AutoCAD.Geometry;
  8. using Autodesk.AutoCAD.EditorInput;
  9. using Autodesk.AutoCAD.GraphicsInterface;
  10. using KdubServices.Tools;
  11. using AcadApp = Autodesk.AutoCAD.ApplicationServices.Core.Application;
  12.  
  13. namespace Linetype
  14. {
  15.     /// <summary>
  16.     /// Generate Complex LinetypeTableRecord to suit text string.
  17.     /// http://www.theswamp.org/index.php?topic=50450.msg556111#msg556111
  18.     ///
  19.     /// </summary>
  20.     public class LtCommands
  21.     {
  22.         [CommandMethod("LT00")]
  23.         public void Test00() {
  24.             Database db = HostApplicationServices.WorkingDatabase;
  25.             ObjectId linetypeId = CreateLinetypeComplexText("ITM-alpha", "ALPHA", db);
  26.             Test11();
  27.             Test12();
  28.             Test13();
  29.         }
  30. //--- [CommandMethod("LT11")]
  31.  
  32. //---[CommandMethod("LT12")]
  33.  
  34. //---[CommandMethod("LT13")]
  35.  
  36. //---public ObjectId CreateLinetypeComplexText(string linetypeName, string includedText, Database db) {
  37.    }
  38. }
  39.  

Code - C#: [Select]
  1.         [CommandMethod("LT11")]
  2.         public void Test11() {
  3.             Database db = HostApplicationServices.WorkingDatabase;
  4.             ObjectId linetypeId = CreateLinetypeComplexText("ITM-ap", "AP", db);
  5.  
  6.             using (Transaction tr = db.TransactionManager.StartTransaction()) {
  7.                 BlockTable bt = tr.GetObject(db.BlockTableId,
  8.                                              OpenMode.ForRead)
  9.                                 as BlockTable;
  10.                 BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace],
  11.                                                     OpenMode.ForWrite)
  12.                                        as BlockTableRecord;
  13.  
  14.                 Line ln = new Line(new Point3d(0, 50, 0),
  15.                                    new Point3d(150, 50, 0));
  16.  
  17.                 ln.SetDatabaseDefaults(db);
  18.                 ln.LinetypeId = linetypeId;
  19.  
  20.                 btr.AppendEntity(ln);
  21.                 tr.AddNewlyCreatedDBObject(ln, true);
  22.                 tr.Commit();
  23.             }
  24.         }
  25.  

Code - C#: [Select]
  1.  
  2.         [CommandMethod("LT12")]
  3.         public void Test12() {
  4.             Database db = HostApplicationServices.WorkingDatabase;
  5.             ObjectId linetypeId = CreateLinetypeComplexText("ITM-apple", "APPLE", db);
  6.  
  7.             using (Transaction tr = db.TransactionManager.StartTransaction()) {
  8.                 BlockTable bt = tr.GetObject(db.BlockTableId,
  9.                                              OpenMode.ForRead)
  10.                                 as BlockTable;
  11.                 BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace],
  12.                                                     OpenMode.ForWrite)
  13.                                        as BlockTableRecord;
  14.  
  15.                 Line ln = new Line(new Point3d(0, 75, 0),
  16.                                    new Point3d(150, 75, 0));
  17.  
  18.                 ln.SetDatabaseDefaults(db);
  19.                 ln.LinetypeId = linetypeId;
  20.  
  21.                 btr.AppendEntity(ln);
  22.                 tr.AddNewlyCreatedDBObject(ln, true);
  23.                 tr.Commit();
  24.             }
  25.         }
  26.  

Code - C#: [Select]
  1.         [CommandMethod("LT13")]
  2.         public void Test13() {
  3.             Database db = HostApplicationServices.WorkingDatabase;
  4.             ObjectId linetypeId = CreateLinetypeComplexText("ITM-applesauce", "APPLESAUCE", db);
  5.  
  6.             using (Transaction tr = db.TransactionManager.StartTransaction()) {
  7.                 BlockTable bt = tr.GetObject(db.BlockTableId,
  8.                                              OpenMode.ForRead)
  9.                                 as BlockTable;
  10.                 BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace],
  11.                                                     OpenMode.ForWrite)
  12.                                        as BlockTableRecord;
  13.  
  14.                 Line ln = new Line(new Point3d(0, 100, 0),
  15.                                    new Point3d(150, 100, 0));
  16.  
  17.                 ln.SetDatabaseDefaults(db);
  18.                 ln.LinetypeId = linetypeId;
  19.  
  20.                 btr.AppendEntity(ln);
  21.                 tr.AddNewlyCreatedDBObject(ln, true);
  22.                 tr.Commit();
  23.             }
  24.         }
  25.  

Code - C#: [Select]
  1.         public ObjectId CreateLinetypeComplexText(string linetypeName, string includedText, Database db) {
  2.             ObjectId linetypeId = ObjectId.Null;
  3.             using (Transaction tr = db.TransactionManager.StartTransaction()) {
  4.                 LinetypeTable ltT = tr.GetObject(db.LinetypeTableId,
  5.                                                  OpenMode.ForWrite)
  6.                                     as LinetypeTable;
  7.  
  8.                 if (ltT.Has(linetypeName)) {
  9.                     linetypeId = ltT[linetypeName];
  10.                     if (linetypeId.IsErased) {
  11.                         LinetypeTableRecord ltTRec = tr.GetObject(linetypeId, OpenMode.ForWrite, true)
  12.                                                      as LinetypeTableRecord;
  13.                         ltTRec.Erase(false);
  14.                     }
  15.                 }
  16.                 else {
  17.                     TextStyleTable tsT = tr.GetObject(db.TextStyleTableId,
  18.                                                       OpenMode.ForRead)
  19.                                          as TextStyleTable;
  20.  
  21.                     LinetypeTableRecord ltTR = new LinetypeTableRecord();
  22.  
  23.                     double ltScale = 1;
  24.                     double txtHeight = 2.5 * ltScale;
  25.                     // 2.5 is character nominal width of "Standard" Style @ 2.5 high
  26.                     double textWidth = includedText.Length * 2.5;
  27.                     double txtYoffset = txtHeight * 0.5;
  28.                     double gapWidth = 2.5;
  29.                     double dash_0 = 13;
  30.                     double dash_1 = gapWidth + (textWidth * 0.5);
  31.                     double dash_2 = dash_1;
  32.  
  33.                     ltTR.Name = linetypeName;
  34.                     ltTR.Comments = " --- " + includedText + " --- ";
  35.                     ltTR.IsScaledToFit = false;
  36.                     ltTR.NumDashes = 3;
  37.  
  38.                     ltTR.PatternLength = dash_0 + dash_1 + dash_2;
  39.  
  40.                     // index Dash[0]
  41.  
  42.                     ltTR.SetDashLengthAt(0, dash_0);
  43.  
  44.                     // index Dash[1]
  45.  
  46.                     ltTR.SetDashLengthAt(1, -dash_1);
  47.                     ltTR.SetShapeStyleAt(1, tsT["Standard"]);
  48.                     ltTR.SetShapeNumberAt(1, 0);
  49.  
  50.                     ltTR.SetShapeOffsetAt(1, new Vector2d(-(textWidth * 0.5), -txtYoffset));
  51.                     ltTR.SetShapeScaleAt(1, txtHeight);
  52.  
  53.                     ltTR.SetShapeIsUcsOrientedAt(1, false);
  54.                     ltTR.SetShapeRotationAt(1, 0);
  55.                     ltTR.SetTextAt(1, includedText);
  56.  
  57.                     // index Dash[2]
  58.  
  59.                     ltTR.SetDashLengthAt(2, -dash_2);
  60.  
  61.                     // Add linetype to the linetype table
  62.  
  63.                     linetypeId = ltT.Add(ltTR);
  64.                     tr.AddNewlyCreatedDBObject(ltTR, true);
  65.                 }
  66.  
  67.                 tr.Commit();
  68.             }
  69.             return linetypeId;
  70.         }
  71.  
« Last Edit: November 23, 2015, 12:12:22 AM by Kerry »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.