.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Write to command line more than once during a command using editor.writeline

12 REPLIES 12
SOLVED
Reply
Message 1 of 13
Keith.Brown
7211 Views, 12 Replies

Write to command line more than once during a command using editor.writeline

I am using editor.writeline to send information to the command line during the execution of a command.  Unfortunately, it will only display the last message written when the command ends.  Is there a way to display all the messages when the command ends?  Below is the code that I am using.  I am new to vb.net in general and in programming autocad so if you see anything that can be done better in the code please let me know.  Thanks in advance.

 

        Public Sub CreateVentilationLayers()

            CreateTrimbleLayer(VENT375)
            CreateTrimbleLayer(VENT500)
            CreateTrimbleLayer(VENT625)
            CreateTrimbleLayer(VENT750)
            CreateTrimbleLayer(VENT875)

        End Sub
        Public Sub CreateTrimbleLayer(ByVal LayerName)

            Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
            Dim myDB As Database = HostApplicationServices.WorkingDatabase

            Try
                Using myTrans As Transaction = myDB.TransactionManager.StartTransaction

                    Dim myLayerTable As LayerTable = myDB.LayerTableId.GetObject(OpenMode.ForWrite)
                    Dim myLayer As New LayerTableRecord
                    myLayer.Name = LayerName
                    myLayer.Color = Colors.Color.FromColor(Drawing.Color.White)
                    myLayer.LineWeight = LineWeight.LineWeight050
                    myLayerTable.Add(myLayer)
                    myLayer.Description = LayerName
                    myTrans.AddNewlyCreatedDBObject(myLayer, True)
                    myTrans.Commit()
                    ed.WriteMessage(LayerName + " layer created..." + Chr(13))
                End Using

            Catch ex As Exception

                ed.WriteMessage(LayerName + " not created..." + Chr(13))

            End Try


        End Sub

 

 

12 REPLIES 12
Message 2 of 13
Keith.Brown
in reply to: Keith.Brown

Also, the following line:

 

myLayer.Color = Colors.Color.FromColor(Drawing.Color.White)

 

will show as 255,255,255 under color in the layer manager.  How do I get it to show white?

Message 3 of 13
kdub_nz
in reply to: Keith.Brown

 

What Type or parameter are you passing to the Sub ... is it meant to be a String ??

 

 

//

Everything will work just as you expect it to, unless your expectations are incorrect.

class keyThumper<T> : Lazy<T>;      another  Swamper

Message 4 of 13
Keith.Brown
in reply to: kdub_nz

Yes,  the parameters are strings that are defined as constants.  I just wanted one place to modify layer names if I had to.  I will give the code a try and see if it works for me.

Message 5 of 13
kdub_nz
in reply to: Keith.Brown

Fourth try at posting the code ... I'm technologically challenged 🙂

Perhaps Convert this and have a play

using System;
using System.Windows.Media;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Colors;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
using AcColors = Autodesk.AutoCAD.Colors;

[assembly: CommandClass(typeof (CreateVentilation.MyCommands))]

namespace CreateVentilation
{
    public class MyCommands
    {
        // Modal Command with localized name
        [CommandMethod("Test02", CommandFlags.Modal)]
        public void CreateVentilationLayers()
        {
            CreateTrimbleLayer("VENT375", 0);
            CreateTrimbleLayer("VENT500", 1);
            CreateTrimbleLayer("VENT625", 2);
            CreateTrimbleLayer("VENT750", 3);
            CreateTrimbleLayer("VENT875", 4);
        }

        public void CreateTrimbleLayer(string layerName, short colorNum)
        {
            var doc = AcadApp.DocumentManager.MdiActiveDocument;
            var db = doc.Database;
            var ed = doc.Editor;

            try
            {
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    var lt = (LayerTable) db.LayerTableId.GetObject(OpenMode.ForWrite);
                    if (lt.Has(layerName))
                    {
                        ed.WriteMessage(layerName + " exists...\n");
                        return;
                    }

                    var ltr = new LayerTableRecord();
                    ltr.Name = layerName;
                    ltr.Color = AcColors.Color.FromColorIndex(ColorMethod.ByAci,
                                                              colorNum);

                    ltr.LineWeight = LineWeight.LineWeight050;
                    lt.Add(ltr);
                    ltr.Description = layerName;

                    tr.AddNewlyCreatedDBObject(ltr, true);
                    tr.Commit();
                    ed.WriteMessage(layerName + " layer created...\n");
                }
            }
            catch (Autodesk.AutoCAD.Runtime.Exception)
            {
                ed.WriteMessage(layerName + " Ooooops ; not created...\n");
            }
        }
    }
}

 

//

Everything will work just as you expect it to, unless your expectations are incorrect.

class keyThumper<T> : Lazy<T>;      another  Swamper

Message 6 of 13
Keith.Brown
in reply to: kdub_nz

Ok, that worked perfect.  Would you mind explaining why so I can use this as a learning experience?   Below is the final code that I used.

 

 

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD
Imports Autodesk.AutoCAD.Colors
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.ApplicationServices.Application
Imports Autodesk.AutoCAD.LayerManager
Imports Autodesk.AutoCAD.Windows
Imports Autodesk.Aec.Building.Piping.DatabaseServices
Imports AecBPropDB = Autodesk.Aec.PropertyData.DatabaseServices
Imports AcApp = Autodesk.AutoCAD.ApplicationServices.Application
Imports AcColors = Autodesk.AutoCAD.Colors



Namespace Trimble

    Public Class Trimble


#Region "Constants"

        Private Const HYD375 As String = "Trimble-Hyd-Insert-0.375"
        Private Const HYD500 As String = "Trimble-Hyd-Insert-0.500"
        Private Const HYD625 As String = "Trimble-Hyd-Insert-0.625"
        Private Const HYD750 As String = "Trimble-Hyd-Insert-0.750"
        Private Const HYD875 As String = "Trimble-Hyd-Insert-0.875"
        Private Const HYDSLV As String = "Trimble-Hyd-Sleeve"
        Private Const HYDSLVDIM As String = "Trimble-Hyd-Sleeve-Dim"
        Private Const HYDSLVTEXT As String = "Trimble-Hyd-Sleeve-Text"
        Private Const HYDSYS As String = "Trimble-Hyd-System"
        Private Const PLBG375 As String = "Trimble-Plbg-Insert-0.375"
        Private Const PLBG500 As String = "Trimble-Plbg-Insert-0.500"
        Private Const PLBG625 As String = "Trimble-Plbg-Insert-0.625"
        Private Const PLBG750 As String = "Trimble-Plbg-Insert-0.750"
        Private Const PLBG875 As String = "Trimble-Plbg-Insert-0.875"
        Private Const PLBGSLV As String = "Trimble-Plbg-Sleeve"
        Private Const PLBGSLVDIM As String = "Trimble-Plbg-Sleeve-Dim"
        Private Const PLBGSLVTEXT As String = "Trimble-Plbg-Sleeve-Text"
        Private Const PLBGSYS As String = "Trimble-Plbg-System"
        Private Const VENT375 As String = "Trimble-Vent-Insert-0.375"
        Private Const VENT500 As String = "Trimble-Vent-Insert-0.500"
        Private Const VENT625 As String = "Trimble-Vent-Insert-0.625"
        Private Const VENT750 As String = "Trimble-Vent-Insert-0.750"
        Private Const VENT875 As String = "Trimble-Vent-Insert-0.875"
        Private Const VENTSLV As String = "Trimble-Vent-Sleeve"
        Private Const VENTSLVDIM As String = "Trimble-Vent-Sleeve-Dim"
        Private Const VENTSLVTEXT As String = "Trimble-Vent-Sleeve-Text"
        Private Const VENTSYS As String = "Trimble-Vent-System"
        Private Const GRID As String = "Trimble-Plans-Grid"
        Private Const ARCH As String = "Trimble-Plans-Arch"
        Private Const STRUC As String = "Trimble-Plans-Struc"
        Private Const CONTROL As String = "Trimble-Control-Points"

#End Region

#Region "Commands"


        <CommandMethod("Trimblelayersadd")> _
        Public Sub AddTrimbleLayers()

            Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor

            Dim GetLayerOptions As PromptKeywordOptions = New PromptKeywordOptions("Which Trimble Layers do you want to add? [Hydronics/Plumbing/Ventilation/All]: ", "Hydronics Plumbing Ventilation All")
            Dim GetLayerResults As PromptResult = ed.GetKeywords(GetLayerOptions)

            If (GetLayerResults.Status = PromptStatus.OK) Then

                Select Case GetLayerResults.StringResult

                    Case "Hydronics"

                        CreateHydronicLayers()
                        CreateMiscLayers()

                    Case "Plumbing"

                        CreatePlumbingLayers()
                        CreateMiscLayers()

                    Case "Ventilation"

                        CreateVentilationLayers()
                        CreateMiscLayers()

                    Case "All"

                        CreateAllLayers()
                        CreateMiscLayers()

                End Select
            End If


        End Sub

        <CommandMethod("TrimblePlumbingLayersAdd")> _
        Public Sub AddTrimblePlumbingLayers()

            CreatePlumbingLayers()
            CreateMiscLayers()

        End Sub

        <CommandMethod("TrimbleHydronicsLayersAdd")> _
        Public Sub AddTrimbleHydronicsLayers()

            CreatePlumbingLayers()
            CreateMiscLayers()

        End Sub

        <CommandMethod("TrimbleVentilationLayersAdd")> _
        Public Sub AddTrimbleVentiliationLayers()

            CreatePlumbingLayers()
            CreateMiscLayers()

        End Sub

        <CommandMethod("TrimbleAllLayersAdd")> _
        Public Sub AddTrimbleAllLayers()

            CreateHydronicLayers()
            CreateVentilationLayers()
            CreatePlumbingLayers()
            CreateMiscLayers()

        End Sub
#End Region

#Region "Helper Functions"


        Public Sub CreateAllLayers()

            CreatePlumbingLayers()
            CreateHydronicLayers()
            CreateVentilationLayers()
            CreateMiscLayers()

        End Sub

        Public Sub CreatePlumbingLayers()

            CreateTrimbleLayer(PLBG375, 0)
            CreateTrimbleLayer(PLBG500, 0)
            CreateTrimbleLayer(PLBG625, 0)
            CreateTrimbleLayer(PLBG750, 0)
            CreateTrimbleLayer(PLBG875, 0)
            CreateTrimbleLayer(PLBGSLV, 0)
            CreateTrimbleLayer(PLBGSLVDIM, 0)
            CreateTrimbleLayer(PLBGSLVTEXT, 0)
            CreateTrimbleLayer(PLBGSYS, 0)

        End Sub

        Public Sub CreateHydronicLayers()

            CreateTrimbleLayer(HYD375, 0)
            CreateTrimbleLayer(HYD500, 0)
            CreateTrimbleLayer(HYD625, 0)
            CreateTrimbleLayer(HYD750, 0)
            CreateTrimbleLayer(HYD875, 0)
            CreateTrimbleLayer(HYDSLV, 0)
            CreateTrimbleLayer(HYDSLVDIM, 0)
            CreateTrimbleLayer(HYDSLVTEXT, 0)
            CreateTrimbleLayer(HYDSYS, 0)

        End Sub

        Public Sub CreateVentilationLayers()

            CreateTrimbleLayer(VENT375, 0)
            CreateTrimbleLayer(VENT500, 0)
            CreateTrimbleLayer(VENT625, 0)
            CreateTrimbleLayer(VENT750, 0)
            CreateTrimbleLayer(VENT875, 0)
            CreateTrimbleLayer(VENTSLV, 0)
            CreateTrimbleLayer(VENTSLVDIM, 0)
            CreateTrimbleLayer(VENTSLVTEXT, 0)
            CreateTrimbleLayer(VENTSYS, 0)

        End Sub

        Public Sub CreateMiscLayers()

            CreateTrimbleLayer(GRID, 0)
            CreateTrimbleLayer(ARCH, 0)
            CreateTrimbleLayer(STRUC, 0)
            CreateTrimbleLayer(CONTROL, 0)

        End Sub

        Public Sub CreateTrimbleLayer(layerName As String, colorNum As Short)
            Dim doc = AcApp.DocumentManager.MdiActiveDocument
            Dim db = doc.Database
            Dim ed = doc.Editor

            Try
                Using tr As Transaction = db.TransactionManager.StartTransaction()
                    Dim lt = DirectCast(db.LayerTableId.GetObject(OpenMode.ForWrite), LayerTable)
                    If lt.Has(layerName) Then
                        ed.WriteMessage(layerName & " exists..." & vbLf)
                        Return
                    End If

                    Dim ltr = New LayerTableRecord()
                    ltr.Name = layerName
                    ltr.Color = AcColors.Color.FromColorIndex(ColorMethod.ByAci, colorNum)

                    ltr.LineWeight = LineWeight.LineWeight050
                    lt.Add(ltr)
                    ltr.Description = layerName

                    tr.AddNewlyCreatedDBObject(ltr, True)
                    tr.Commit()
                    ed.WriteMessage(layerName & " layer created..." & vbLf)
                End Using
            Catch generatedExceptionName As Autodesk.AutoCAD.Runtime.Exception
                ed.WriteMessage(layerName & " Ooooops ; not created..." & vbLf)
            End Try
        End Sub
#End Region

    End Class

End Namespace

 

 

 

 

Message 7 of 13
kdub_nz
in reply to: Keith.Brown

 

>> Ok, that worked perfect.  Would you mind explaining why so I can use this as a learning experience?

 

Sorry, no ... my brain stops working when I try to debug VB.

 

//

Everything will work just as you expect it to, unless your expectations are incorrect.

class keyThumper<T> : Lazy<T>;      another  Swamper

Message 8 of 13
Keith.Brown
in reply to: kdub_nz

The only difference I see is the way that i was creating my database object.  I used the way that was outlined inside of Jerry Winter's VB book.  I will start using the way you defined yours. 

Message 9 of 13

The one difference stands out to me is that Kerry performs a check to see if the layer exists, and as you mentioned the way the db is created.

Message 10 of 13
kdub_nz
in reply to: Keith.Brown


@advmech_kbrown wrote:

>> Ok, that worked perfect.  Would you mind explaining why so I can use this as a learning experience?   Below is the final code that I used.

Something else to consider ..
You may want to pass the LineWeight enumerator to
CreateTrimbleLayer()
.. This will allow your generic layer builder to set the Lineweight from the enumerator instead of having each Layer use the Hardcoded LineWeight.
Regards,
kdub
//

Everything will work just as you expect it to, unless your expectations are incorrect.

class keyThumper<T> : Lazy<T>;      another  Swamper

Message 11 of 13
Keith.Brown
in reply to: kdub_nz

Good Suggestion,  I had actually already added this to the program.  Below is the current code that I am using to add the layers.

 

'This sub will accept a layername, a color number, a lineweight and then create the layer.
        Public Sub CreateTrimbleLayer(layerName As String, colorNum As Short, LineWeightNum As LineWeight)

            'Get the editor object
            Dim doc = AcApp.DocumentManager.MdiActiveDocument
            Dim db = doc.Database
            Dim ed = doc.Editor

            'Create the layer.  If there is an error then catch it.
            Try
                'Start the database transaction
                Using tr As Transaction = db.TransactionManager.StartTransaction()

                    'Open the LayerTable for write
                    Dim lt = DirectCast(db.LayerTableId.GetObject(OpenMode.ForWrite), LayerTable)

                    'If the layer is already in the database then write a message to the command
                    'line and return
                    If lt.Has(layerName) Then
                        ed.WriteMessage(layerName & " exists..." & vbLf)
                        Return
                    End If

                    'Create a new LayerTableRecord and set the name, color, and lineweight
                    Dim ltr = New LayerTableRecord()
                    ltr.Name = layerName
                    ltr.Color = AcColors.Color.FromColorIndex(ColorMethod.ByAci, colorNum)
                    ltr.LineWeight = LineWeightNum

                    'Add the LayerTableRecord to the LayerTable
                    lt.Add(ltr)

                    'Set the Layer Description equal to the Layer Name
                    ltr.Description = layerName

                    'Add the created object to the database and commit the transaction
                    tr.AddNewlyCreatedDBObject(ltr, True)
                    tr.Commit()

                    'The layer has been successfully created so write a message to the commandline
                    ed.WriteMessage(layerName & " layer created..." & vbLf)
                End Using

                'There was an unknown error when creating the layer so write a message to the commandline
            Catch generatedExceptionName As Autodesk.AutoCAD.Runtime.Exception
                ed.WriteMessage(layerName & " Ooooops ; not created..." & vbLf)
            End Try
        End Sub

 

Message 12 of 13
Keith.Brown
in reply to: kdub_nz

Kerry,

 

I see that you also post alot on theswamp.org.  Do you have any suggestions on the problems I have encountered in this thread?  Thanks in advance!

 

how to access pipe information

Message 13 of 13
kdub_nz
in reply to: Keith.Brown

 

Yeah, I feel at home @ theSwamp 🙂

 

 

I 'normally' stick with C# code. I just happened to scan your code and couldn't see an obvious reason it should act as you described ... so I hacked out the C# to see how I'd have attacked it.

 

Your code seems to be layed out well (modular)  and should lend itself to debugging and expansion ... which is important as I see it.

 

Sugestions ?  🙂  just keep bashing keys.

Regards

 

 

//

Everything will work just as you expect it to, unless your expectations are incorrect.

class keyThumper<T> : Lazy<T>;      another  Swamper

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost