.NET

.NET

Reply
*Expert Elite*
Keith.Brown
Posts: 1,237
Registered: ‎03-13-2008
Message 1 of 13 (792 Views)
Accepted Solution

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

792 Views, 12 Replies
01-28-2012 04:19 PM

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

 

 


Keith Brown AutoCAD MEP BLOG | RSS Feed
AutoCAD MEP 2014 | Revit 2014 | EastCoast CAD/CAM V6.1 | Visual Studio 2013
────────────────────────────────────
⁞|⁞ Please use Mark Solutions!.Accept as Solution and Give Kudos!Give Kudos as appropriate. Thank you!

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

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");
}
}
}
}

 

*Expert Elite*
Keith.Brown
Posts: 1,237
Registered: ‎03-13-2008
Message 2 of 13 (784 Views)

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

01-28-2012 05:44 PM 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?


Keith Brown AutoCAD MEP BLOG | RSS Feed
AutoCAD MEP 2014 | Revit 2014 | EastCoast CAD/CAM V6.1 | Visual Studio 2013
────────────────────────────────────
⁞|⁞ Please use Mark Solutions!.Accept as Solution and Give Kudos!Give Kudos as appropriate. Thank you!
Valued Mentor
KerryBrown
Posts: 278
Registered: ‎11-29-2008
Message 3 of 13 (782 Views)

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

01-28-2012 05:52 PM 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

*Expert Elite*
Keith.Brown
Posts: 1,237
Registered: ‎03-13-2008
Message 4 of 13 (779 Views)

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

01-28-2012 05:54 PM in reply to: KerryBrown

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.


Keith Brown AutoCAD MEP BLOG | RSS Feed
AutoCAD MEP 2014 | Revit 2014 | EastCoast CAD/CAM V6.1 | Visual Studio 2013
────────────────────────────────────
⁞|⁞ Please use Mark Solutions!.Accept as Solution and Give Kudos!Give Kudos as appropriate. Thank you!
Valued Mentor
KerryBrown
Posts: 278
Registered: ‎11-29-2008
Message 5 of 13 (776 Views)

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

01-28-2012 05:58 PM in reply to: Keith.Brown

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

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

*Expert Elite*
Keith.Brown
Posts: 1,237
Registered: ‎03-13-2008
Message 6 of 13 (764 Views)

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

01-28-2012 06:18 PM in reply to: KerryBrown

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

 

 

 

 


Keith Brown AutoCAD MEP BLOG | RSS Feed
AutoCAD MEP 2014 | Revit 2014 | EastCoast CAD/CAM V6.1 | Visual Studio 2013
────────────────────────────────────
⁞|⁞ Please use Mark Solutions!.Accept as Solution and Give Kudos!Give Kudos as appropriate. Thank you!
Valued Mentor
KerryBrown
Posts: 278
Registered: ‎11-29-2008
Message 7 of 13 (758 Views)

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

01-28-2012 06:46 PM 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

*Expert Elite*
Keith.Brown
Posts: 1,237
Registered: ‎03-13-2008
Message 8 of 13 (752 Views)

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

01-28-2012 07:50 PM in reply to: KerryBrown

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. 


Keith Brown AutoCAD MEP BLOG | RSS Feed
AutoCAD MEP 2014 | Revit 2014 | EastCoast CAD/CAM V6.1 | Visual Studio 2013
────────────────────────────────────
⁞|⁞ Please use Mark Solutions!.Accept as Solution and Give Kudos!Give Kudos as appropriate. Thank you!
Mentor
BrentBurgess1980
Posts: 166
Registered: ‎06-16-2008
Message 9 of 13 (727 Views)

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

01-29-2012 03:55 PM in reply to: Keith.Brown

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.

Valued Mentor
KerryBrown
Posts: 278
Registered: ‎11-29-2008
Message 10 of 13 (720 Views)

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

01-29-2012 07:19 PM 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

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.