Author Topic: Undefine/Redefine Xref  (Read 2673 times)

0 Members and 1 Guest are viewing this topic.

cadpro

  • Guest
Undefine/Redefine Xref
« on: March 22, 2007, 06:00:58 AM »
Hi,

I would like to create a VBA module to undefine Xref command, then create a new command called 'xref', then redefine it. Is this possible? Please help.

Thanks

Glenn R

  • Water Moccasin
  • Posts: 1932
  • What idiot child of married cousins wrote this?!
Re: Undefine/Redefine Xref
« Reply #1 on: March 22, 2007, 09:05:07 AM »
No.
Me

cadpro

  • Guest
Re: Undefine/Redefine Xref
« Reply #2 on: March 27, 2007, 01:25:58 AM »
Redefine...in fact is not necessary...when a user types xref in the command line, immedietly the ActiveUCS should go to 'World', as well as the ActiveLayer should go to '0'. After that the Xref dialog box should pop up. After inserting the drawing through the xref dialog box, the layer and ucs should go back to the original as it was earlier. Can this be accomplished?

Thanks

Dave R

  • Guest
Re: Undefine/Redefine Xref
« Reply #3 on: March 27, 2007, 08:15:43 AM »
Yes, you can use the BeginCommand event of the active document to do this.

Redefine...in fact is not necessary...when a user types xref in the command line, immedietly the ActiveUCS should go to 'World', as well as the ActiveLayer should go to '0'. After that the Xref dialog box should pop up. After inserting the drawing through the xref dialog box, the layer and ucs should go back to the original as it was earlier. Can this be accomplished?

Thanks

Bryco

  • Water Moccasin
  • Posts: 1851
Re: Undefine/Redefine Xref
« Reply #4 on: March 27, 2007, 10:12:00 AM »
Don't forget XATTACH as well.

cadpro

  • Guest
Re: Undefine/Redefine Xref
« Reply #5 on: March 28, 2007, 12:32:13 AM »
This is the code I wrote. But unfortunately, it's not working.
Please help.

Code: [Select]
Option Explicit
Dim CurUCS As AcadUCS
Dim CurLayer As AcadLayer

Private Sub AcadDocument_BeginCommand(ByVal CommandName As String)

If CommandName = "XREF" Or CommandName = "XATTACH" Then
Set CurUCS = ThisDrawing.ActiveUCS
Set CurLayer = ThisDrawing.ActiveLayer
SendKeys "{Esc}"
ThisDrawing.SendCommand "ucs" & vbCr & "world" & vbCr
ThisDrawing.Layers("0").Freeze = False
ThisDrawing.Layers("0").LayerOn = True
ThisDrawing.ActiveLayer = ThisDrawing.Layers("0")
End If

End Sub

Private Sub AcadDocument_EndCommand(ByVal CommandName As String)

If CommandName = "XREF" Or CommandName = "XATTACH" Then
ThisDrawing.ActiveUCS = CurUCS
ThisDrawing.ActiveLayer = CurLayer
End If

End Sub
Thanks

Dave R

  • Guest
Re: Undefine/Redefine Xref
« Reply #6 on: March 28, 2007, 08:15:58 AM »
I don't think you're going to be able to effectively use the sendkeys function while you are beginning another command. You may want to take a look at programmatically setting your UCS rather than using the sendkeys function.

This is the code I wrote. But unfortunately, it's not working.
Please help.

Code: [Select]
Option Explicit
Dim CurUCS As AcadUCS
Dim CurLayer As AcadLayer

Private Sub AcadDocument_BeginCommand(ByVal CommandName As String)

If CommandName = "XREF" Or CommandName = "XATTACH" Then
Set CurUCS = ThisDrawing.ActiveUCS
Set CurLayer = ThisDrawing.ActiveLayer
SendKeys "{Esc}"
ThisDrawing.SendCommand "ucs" & vbCr & "world" & vbCr
ThisDrawing.Layers("0").Freeze = False
ThisDrawing.Layers("0").LayerOn = True
ThisDrawing.ActiveLayer = ThisDrawing.Layers("0")
End If

End Sub

Private Sub AcadDocument_EndCommand(ByVal CommandName As String)

If CommandName = "XREF" Or CommandName = "XATTACH" Then
ThisDrawing.ActiveUCS = CurUCS
ThisDrawing.ActiveLayer = CurLayer
End If

End Sub
Thanks

Bryco

  • Water Moccasin
  • Posts: 1851
Re: Undefine/Redefine Xref
« Reply #7 on: March 28, 2007, 10:24:27 AM »
This appears to work.
Code: [Select]
Sub owcs()

    Dim zero(2) As Double
    Dim Xaxis(2) As Double
    Dim Yaxis(2) As Double
    Dim strNm As String, sUcs As String
    Dim oUcs As AcadUCS
   
    If ThisDrawing.GetVariable("WORLDUCS") = 1 Then
        Exit Sub
    Else
        sUcs = ThisDrawing.GetVariable("UCSNAME")
        'Debug.Print sUcs
        Xaxis(0) = 1: Yaxis(1) = 1
        Set oUcs = ThisDrawing.UserCoordinateSystems.Add(zero, Xaxis, Yaxis, "World")
        ThisDrawing.ActiveUCS = oUcs
    End If
End Sub

cadpro

  • Guest
Re: Undefine/Redefine Xref
« Reply #8 on: March 28, 2007, 11:57:56 PM »
I duplicated WCS and it all works fine. But one problem. If the user clicks on the Xref dialog box's Cancel button, the old UCS and Layer are not restored as it doesn't execute the EndCommand Event. I have given the code to restore the old UCS and Layer in the EndCommand event. Is there a workaround?

Thanks

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16785
  • Superior Stupidity at its best
Re: Undefine/Redefine Xref
« Reply #9 on: March 29, 2007, 12:26:52 AM »
I duplicated WCS and it all works fine. But one problem. If the user clicks on the Xref dialog box's Cancel button, the old UCS and Layer are not restored as it doesn't execute the EndCommand Event. I have given the code to restore the old UCS and Layer in the EndCommand event. Is there a workaround?

Thanks

Something that has worked for me in the past, is to set a configuration variable in the begincommand event, then clear it in the end command event ... filtering of course for the proper command names ... now in the begin command event, if the variable is already set, then clear it and reset the previous settings saved in the last firing of the begincommand event.

The flow would be something like this:
[Begin Command]
<check config setting>
if settings = true
{
   <reset drawing vars>
   <clear config setting>
}
<verify xref call>
<set config settings>
<save current settings>
<change drawing vars>
xref command runs

[End Command]
<verify xref call>
<reset drawing vars>
<clear config setting>


This scenario would reset the previous settings that didn't get reset when xref was cancelled, immediately upon running another command .. beware however, you would also need to verify the settings using the Begin Lisp event too in order to capture most scenarios.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

cadpro

  • Guest
Re: Undefine/Redefine Xref
« Reply #10 on: April 01, 2007, 12:35:03 AM »
Keith,

I'm sorry I didn't understand the workaround you explained. I will give the code that I have done. Hope you will do the changes in the code, and that makes me understand better.

Code: [Select]
Option Explicit
Dim CurUCS As AcadUCS
Dim CurLayer As AcadLayer
Dim UCSs As Object

Private Sub AcadDocument_BeginCommand(ByVal CommandName As String)

If CommandName = "XREF" Or CommandName = "XATTACH" Then
    Set CurUCS = ThisDrawing.ActiveUCS
    Set CurLayer = ThisDrawing.ActiveLayer
    Call ShowWCS
    ThisDrawing.Layers("0").Freeze = False
    ThisDrawing.Layers("0").LayerOn = True
    ThisDrawing.ActiveLayer = ThisDrawing.Layers("0")
End If

End Sub
Sub ShowWCS()
'
'   Display WCS
'
    Dim wcs As Object
    Dim dorigin(0 To 2) As Double
    Dim dxAxisPnt(0 To 2) As Double
    Dim dyAxisPnt(0 To 2) As Double
       
    dorigin(0) = 0#
    dorigin(1) = 0#
    dorigin(2) = 0#
   
    dxAxisPnt(0) = 1#
    dxAxisPnt(1) = 0#
    dxAxisPnt(2) = 0#
   
    dyAxisPnt(0) = 0#
    dyAxisPnt(1) = 1#
    dyAxisPnt(2) = 0#
   
    Set wcs = ThisDrawing.UserCoordinateSystems.Add(dorigin, dxAxisPnt, dyAxisPnt, "WORLD")

'    Display WCS.
    ThisDrawing.ActiveUCS = wcs
   
End Sub

Private Sub AcadDocument_EndCommand(ByVal CommandName As String)
If CommandName = "XREF" Or CommandName = "XATTACH" Then
    ThisDrawing.ActiveUCS = CurUCS
    ThisDrawing.ActiveLayer = CurLayer
    ThisDrawing.UserCoordinateSystems.Item("World").Delete
End If

End Sub

If I am not wrong, I understood that the execution control never enters the EndCommand Event if the Xref dialog box is cancelled. Please help.

Thanks

Bryco

  • Water Moccasin
  • Posts: 1851
Re: Undefine/Redefine Xref
« Reply #11 on: April 01, 2007, 01:38:47 AM »
dim bActive  as boolean
set this true in your startcommand
set it false in your endcommand
if it is true when you start the startcommand then the endcommand was never activated (an escape happened). Now you reset the layers

cadpro

  • Guest
Re: Undefine/Redefine Xref
« Reply #12 on: April 02, 2007, 01:51:08 AM »
Bryco,

I did what you said in the previous post, but didn't work. In the third step, you wrote, If true in StartCommand, then EndCommand never activated. So my question is, if EndCommand never activated, then the boolean is true, which is set in startcommand. so the snippet that is needed will not run.

Thanks

Bryco

  • Water Moccasin
  • Posts: 1851
Re: Undefine/Redefine Xref
« Reply #13 on: April 02, 2007, 10:48:47 PM »
Watch the layer manager as you use the Line command ,escape yadayada

Code: [Select]
Option Explicit

Private Curlayer As AcadLayer
Private bActive As Boolean

Private Sub AcadDocument_BeginCommand(ByVal CommandName As String)
    If bActive Then
        ThisDrawing.ActiveLayer = Curlayer
    End If
   
    If CommandName = "LINE" Then
        Set Curlayer = ThisDrawing.ActiveLayer
        ThisDrawing.ActiveLayer = ThisDrawing.Layers("0")
        bActive = True
    End If
End Sub



Private Sub AcadDocument_EndCommand(ByVal CommandName As String)
    If CommandName = "LINE" Then
        bActive = False
        ThisDrawing.ActiveLayer = Curlayer
    End If
End Sub

cadpro

  • Guest
Re: Undefine/Redefine Xref
« Reply #14 on: April 12, 2007, 01:18:29 AM »
No! Didn't work.

Bryco

  • Water Moccasin
  • Posts: 1851
Re: Undefine/Redefine Xref
« Reply #15 on: April 12, 2007, 01:36:05 AM »
That's odd works for me on 2000,2004,2006,7 &8 The instructions do include yadayada which actually means you do something else after you escape. But net will be the answer I guess.