• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    .NET

    Reply
    Member
    Posts: 4
    Registered: ‎05-12-2008

    Change layer per command

    329 Views, 9 Replies
    01-17-2012 04:27 PM

    I have a VBA program that I have been using for years that I am unable to convert to vb.net. I figure before I start beating my head on the keyboard I would ask if anyone has anything similar. When I start a text/dim command, the routine changes my layer to my Dimension layer, like wise when I create a viewport. Anyone have something similar that I can look at the code.

     

    I like what I see with A2k12 but all this vb.net stuff has me quite frustrated. So many programs to convert so little time

     

    TIA.

    Kevin

     

    Please use plain text.
    Valued Contributor
    SENL1362
    Posts: 58
    Registered: ‎07-20-2011

    Re: Change layer per command

    01-17-2012 11:31 PM in reply to: kulmsr

    Why would you want to do that through code ? Place the command on a ToolPalette and fill in the required layer. Far more flexible.

    Please use plain text.
    *Expert Elite*
    Posts: 6,463
    Registered: ‎06-29-2007

    Re: Change layer per command

    01-18-2012 01:04 AM in reply to: kulmsr

    Hi,

     

    set the sysvar CLAYER to your layer.

    And if the layer does not exist, you have to create it before (as you did it with your VBA).

     

    >> but all this vb.net stuff has me quite frustrated

    As all in the live you have to learn it and you have to get used to it. Yes ... vb.NET is different to VBA, but the more you get used to it you will see so much additional functionality and speed. Bear up - it's great then.

     

    - alfred -

    -------------------------------------------------------------------------
    Alfred NESWADBA
    Ingenieur Studio HOLLAUS ... www.hollaus.at
    -------------------------------------------------------------------------
    Please use plain text.
    Member
    Posts: 4
    Registered: ‎05-12-2008

    Re: Change layer per command

    01-18-2012 06:19 AM in reply to: kulmsr

    Because I'm old and resistant to change.:smileyhappy:

     

    Actually, I am old school keyboard. I still use the PGP file extensivly. I have started using pallets primarily for blocks but find it quicker to type my 2 characters over looking for the appropriate icon. If push comes to shove I will look in that direction.

     

    Thanks

    Kevin

    Please use plain text.
    Member
    Posts: 4
    Registered: ‎05-12-2008

    Re: Change layer per command

    01-18-2012 06:28 AM in reply to: alfred.neswadba

    alfred.neswadba wrote:
    >> but all this vb.net stuff has me quite frustrated

    As all in the live you have to learn it and you have to get used to it. Yes ... vb.NET is different to VBA, but the more you get used to it you will see so much additional functionality and speed. Bear up - it's great then.

     

    - alfred -



    I understand that, and am working on about a dozen company specific programs, but it is slow going. I am picking up bits and pieces as I go. As I mentioned in my OP, I was looking to see if someone else had already taken a stab at this so as to not have to start from scratch.

     

    I remember the frustration when I started coding with VBA but I got used to it, the difference here is that I still had my old Lisp programs availible until I got something else working. With this I don't have the option to use the old while working on the new.

     

    Thanks,

    Kevin

    Please use plain text.
    *Expert Elite*
    chiefbraincloud
    Posts: 736
    Registered: ‎02-13-2008

    Re: Change layer per command

    01-18-2012 11:16 AM in reply to: kulmsr

    I'm affraid your original post leaves a few unanswered questions that make it difficult for me to guess at exactly what you're looking for. 

     

    ie. Do you mean that your VBA code "reacts" to the standard autocad text/mtext command and changes the layer, or is it a custom VBA text/mtext command, that changes the layer and then (fires the AutoCAD command or whatever)?

     

    The best thing would probably be to post an example of your VBA code, then I'm sure someone here, myself or otherwise, will be able to help you get it figured out.

     

    On a different note, I have been going at it with VB.NET for several years now, and I no longer have any code that uses the COM libraries (compared to the managed libraries, the COM interface is super slow), but to expedite the conversion process, if using the COM libraries in .NET, the code will be extremely similar to the VBA code, as in, you just copy the VBA code, paste it into your .NET project, and fix whatever errors come up, which will usually be minor errors resulting from the .NET code needing a different variable type, or some such thing.  (like variant => object)

     

    For what it's worth, I don't like tool palettes either, or the ribbon.  They take up too much valuable screen real estate.

    Dave O.                                                                                Sig-Logos32.png
    Please use plain text.
    Valued Contributor
    SENL1362
    Posts: 58
    Registered: ‎07-20-2011

    Re: Change layer per command

    01-19-2012 12:04 AM in reply to: chiefbraincloud

    For what it's worth, I don't like tool palettes either, or the ribbon.  They take up too much valuable screen real estate.

    I myself agree, but our engineers like it because they can change them self a lot easier than the old MNS toolbars.

    The screen size is not a problem because they all have 2 screens -- like in the early '80s

     

    Can i ask you why you choose to VB.NET instead of C# ?

    Please use plain text.
    Member
    Posts: 4
    Registered: ‎05-12-2008

    Re: Change layer per command

    01-19-2012 06:14 AM in reply to: chiefbraincloud

    chiefbraincloud wrote:

    I'm affraid your original post leaves a few unanswered questions that make it difficult for me to guess at exactly what you're looking for. 

     

    ie. Do you mean that your VBA code "reacts" to the standard autocad text/mtext command and changes the layer, or is it a custom VBA text/mtext command, that changes the layer and then (fires the AutoCAD command or whatever)?

     

    Yes, If I exicute a command (defined in the vba) the layer will change to the appropriate layer for that command and then revert to the previous active layer apon completion.

     

    The best thing would probably be to post an example of your VBA code, then I'm sure someone here, myself or otherwise, will be able to help you get it figured out.

     

    Watch out here it comes:


    Option Explicit

        Private cLayer As AcadLayer
        Private newLayer As AcadLayer
        Private tempCmdName As String

    Private Sub AcadDocument_BeginCommand(ByVal CommandName As String)
        On Error Resume Next
        Select Case CommandName
                Case "QDIM"
                Set cLayer = ActiveLayer
                tempCmdName = CommandName
                Set newLayer = ThisDrawing.Layers.Item("DIMS")
                If Err Then
                    Set newLayer = Layers.Add("DIMS")
                    newLayer.Color = 3
                End If
                ActiveLayer = newLayer
            Case "DIMLINEAR"
                Set cLayer = ActiveLayer
                tempCmdName = CommandName
                Set newLayer = ThisDrawing.Layers.Item("DIMS")
                If Err Then
                    Set newLayer = Layers.Add("DIMS")
                    newLayer.Color = 3
                End If
                ActiveLayer = newLayer
            Case "QLEADER"
                Set cLayer = ActiveLayer
                tempCmdName = CommandName
                Set newLayer = ThisDrawing.Layers.Item("DIMS")
                If Err Then
                    Set newLayer = Layers.Add("DIMS")
                    newLayer.Color = 3
                End If
                ActiveLayer = newLayer
            Case "DIMCONTINUE"
                Set cLayer = ActiveLayer
                tempCmdName = CommandName
                Set newLayer = ThisDrawing.Layers.Item("DIMS")
                If Err Then
                    Set newLayer = Layers.Add("DIMS")
                    newLayer.Color = 3
                End If
                ActiveLayer = newLayer
            Case "DIMDIAMETER"
                Set cLayer = ActiveLayer
                tempCmdName = CommandName
                Set newLayer = ThisDrawing.Layers.Item("DIMS")
                If Err Then
                    Set newLayer = Layers.Add("DIMS")
                    newLayer.Color = 3
                End If
                ActiveLayer = newLayer
            Case "DIMANGULAR"
                Set cLayer = ActiveLayer
                tempCmdName = CommandName
                Set newLayer = ThisDrawing.Layers.Item("DIMS")
                If Err Then
                    Set newLayer = Layers.Add("DIMS")
                    newLayer.Color = 3
                End If
                ActiveLayer = newLayer
            Case "DIMRADIUS"
                Set cLayer = ActiveLayer
                tempCmdName = CommandName
                Set newLayer = ThisDrawing.Layers.Item("DIMS")
                If Err Then
                    Set newLayer = Layers.Add("DIMS")
                    newLayer.Color = 3
                End If
                ActiveLayer = newLayer
            Case "DIMORDINATE"
                Set cLayer = ActiveLayer
                tempCmdName = CommandName
                Set newLayer = ThisDrawing.Layers.Item("DIMS")
                If Err Then
                    Set newLayer = Layers.Add("DIMS")
                    newLayer.Color = 3
                End If
                ActiveLayer = newLayer
            Case "DIMBASELINE"
                Set cLayer = ActiveLayer
                tempCmdName = CommandName
                Set newLayer = ThisDrawing.Layers.Item("DIMS")
                If Err Then
                    Set newLayer = Layers.Add("DIMS")
                    newLayer.Color = 3
                End If
                ActiveLayer = newLayer
            Case "DIMALIGNED"
                Set cLayer = ActiveLayer
                tempCmdName = CommandName
                Set newLayer = ThisDrawing.Layers.Item("DIMS")
                If Err Then
                    Set newLayer = Layers.Add("DIMS")
                    newLayer.Color = 3
                End If
                ActiveLayer = newLayer
        Case "TOLERANCE"
                Set cLayer = ActiveLayer
                tempCmdName = CommandName
                Set newLayer = ThisDrawing.Layers.Item("DIMS")
                If Err Then
                    Set newLayer = Layers.Add("DIMS")
                    newLayer.Color = 3
                End If
                ActiveLayer = newLayer
         Case "MVIEW"
                Set cLayer = ActiveLayer
                tempCmdName = CommandName
                Set newLayer = ThisDrawing.Layers.Item("VIEWPORTS")
                If Err Then
                    Set newLayer = Layers.Add("VIEWPORTS")
                    newLayer.Color = acWhite
                    End If
                ActiveLayer = newLayer
        Case "-VPORTS"
                Set cLayer = ActiveLayer
                tempCmdName = CommandName
                Set newLayer = ThisDrawing.Layers.Item("VIEWPORTS")
                If Err Then
                    Set newLayer = Layers.Add("VIEWPORTS")
                    newLayer.Color = acWhite
                    End If
                ActiveLayer = newLayer
            Case "MTEXT"
                Set cLayer = ActiveLayer
                tempCmdName = CommandName
                Set newLayer = ThisDrawing.Layers.Item("DIMS")
                If Err Then
                    Set newLayer = Layers.Add("DIMS")
                    newLayer.Color = 3
                End If
                ActiveLayer = newLayer
            Case "DTEXT"
                Set cLayer = ActiveLayer
                tempCmdName = CommandName
                Set newLayer = ThisDrawing.Layers.Item("DIMS")
                If Err Then
                    Set newLayer = Layers.Add("DIMS")
                    newLayer.Color = 3
                End If
                ActiveLayer = newLayer
            Case "TEXT"
                Set cLayer = ActiveLayer
                tempCmdName = CommandName
                Set newLayer = ThisDrawing.Layers.Item("DIMS")
                If Err Then
                    Set newLayer = Layers.Add("DIMS")
                    newLayer.Color = 3
                End If
                ActiveLayer = newLayer
              End Select
    End Sub

    Private Sub AcadDocument_EndCommand(ByVal CommandName As String)
        On Error Resume Next
        If CommandName = tempCmdName Then
            ActiveLayer = cLayer
        End If
    End Sub


     

    On a different note, I have been going at it with VB.NET for several years now, and I no longer have any code that uses the COM libraries (compared to the managed libraries, the COM interface is super slow), but to expedite the conversion process, if using the COM libraries in .NET, the code will be extremely similar to the VBA code, as in, you just copy the VBA code, paste it into your .NET project, and fix whatever errors come up, which will usually be minor errors resulting from the .NET code needing a different variable type, or some such thing.  (like variant => object)

     

    I have heard that But I am still trying to feel my way thru. As I do not have a lot of time to dedicate to training myself currently I will be looking at that as time allows.

     

    For what it's worth, I don't like tool palettes either, or the ribbon.  They take up too much valuable screen real estate.

     

    AMEN!

     

    Thanks,

    Kevin


     

    Please use plain text.
    *Expert Elite*
    chiefbraincloud
    Posts: 736
    Registered: ‎02-13-2008

    Re: Change layer per command

    01-19-2012 11:02 AM in reply to: SENL1362

    SENL1362 -

     

    I am an old school VB (4,5,6)  programmer, so for starters it's a "stick with what you know" kind of thing, but I looked into learning C#, and discovered that there was not really any benefit over VB, so I never bothered.

     

    A lot of people talk about there being more code examples in C#, but first of all, I know C# syntax well enough to read the examples and see what they are doing, and there are plenty of online code translators to convert them to VB, so I'll just stick to VB.

    Dave O.                                                                                Sig-Logos32.png
    Please use plain text.
    Valued Contributor
    SENL1362
    Posts: 58
    Registered: ‎07-20-2011

    Re: Change layer per command

    01-19-2012 11:48 AM in reply to: chiefbraincloud

    Thank you for answering. I too used VB(A) for the last 10 years and before that C and Fortran.

    Last year started VB.NET but switched to c# because of the better examples and explenations to the how to questions specially when i comes to LINQ and Reflection.

     

    Gr

     

    Please use plain text.