Entity Creation Order

Discussion in 'AutoCAD' started by Tom Roberts, Jul 30, 2004.

  1. Tom Roberts

    Tom Roberts Guest

    Is there a way of determining which of two entities was created last.

    I am attaching xdata to blocks and am trying to get around the problem of
    the User copying a block and therefore creating duplicate xdata.

    I hope to prevent this by running my own audit command when a drawing is
    opened and if duplicate xdata is found moving the last created block to an
    error layer

    Thanks is advance
    --
    Regards
    Tom Roberts
    __________________________
    MechWest Design & Drafting
    Perth, Western Australia
     
    Tom Roberts, Jul 30, 2004
    #1
  2. Tom Roberts

    Mark Propst Guest

    compare their handles
     
    Mark Propst, Jul 30, 2004
    #2
  3. Tom Roberts

    Tom Roberts Guest

    I insert a block its handle=9C
    I insert another instance of the same block and its handle = C7

    If you can explain to me the logic behind how handles are assigned then
    comparing their handles may be a valid option.
     
    Tom Roberts, Jul 30, 2004
    #3
  4. Hi Tom,

    There are probably hexadecimal numbers.

    9C = 9*16 + 12 in decimal terms.
    C7= 12*16 + 7

    1 =1
    2=2
    ....
    A = 10
    B = 11
    --
    F = 15
    10 = 16
    etc

    You should be able to find comparison operations with the numbers in that
    format.

    --


    Laurie Comerford
    CADApps
    www.cadapps.com.au
     
    Laurie Comerford, Jul 30, 2004
    #4
  5. Tom Roberts

    Jürg Menzi Guest

    Hi Tom

    Convert Hex to Decimal with this function:

    Code:
    Public Function Hex2Dec(HexVal As String) As Variant
    
    Dim PosCnt As Integer
    Dim StrLen As Integer
    Dim TmpVal As Integer
    Dim CurDig As Variant
    Dim RetVal As Variant
    
    PosCnt = 1
    StrLen = Len(HexVal)
    
    Do
    CurDig = UCase(Mid(HexVal, PosCnt, 1))
    TmpVal = IIf(Asc(CurDig) > 64, Asc(CurDig) - 55, CVar(CurDig))
    RetVal = RetVal + (TmpVal * (16 ^ (StrLen - PosCnt)))
    PosCnt = PosCnt + 1
    Loop Until PosCnt > StrLen
    
    Hex2Dec = CDec(RetVal)
    
    End Function
    
    Cheers
     
    Jürg Menzi, Jul 30, 2004
    #5
  6. Tom Roberts

    Mark Propst Guest

    Hi Tom,
    Handles get assigned in order of object creation.
    They never change from dwg session to dwg session in a given dwg.
    They are never duplicated,(as far as I understand)
    So they appear to be safe references to track objects.
    Here's what I came up with thanks to Juerg's generous offer of the Hex2Dec
    function

    'test sub to demonstrate usage of comparing handles
    'to try this, draw two lines
    'put each one on a different layer
    'run the sub and pick the two lines

    'using Juerg Menzi's Generously donated function of Hex2Dec


    'test sub
    Public Sub testGetYoungerEntity()
    Dim oline As AcadLine
    Dim oline2 As AcadLine
    Dim oYounger As AcadEntity

    Set oline = adcxGetEnt("Pick Line")
    Set oline2 = adcxGetEnt("Pick Line")
    Set oYounger = YoungerEntity(oline, oline2)

    Debug.Print "Younger entity is on layer: " & oYounger.Layer
    msgbox "Younger entity is on layer: " & oYounger.Layer

    Set oline = Nothing
    Set oline2 = Nothing
    Set oYounger = Nothing

    End Sub



    'quicky selection wrapper to simplify look of main code block - not done yet
    Public Function ADCXGetEnt(strPrmpt As String) As AcadEntity
    Dim rtnEnt As AcadEntity
    Dim vpt As Variant

    'error code goes here
    ThisDrawing.Utility.GetEntity rtnEnt, vpt, strPrmpt
    If Not rtnEnt Is Nothing Then
    Set ADCXGetEnt = rtnEnt
    Else
    MsgBox "GetEntity failed", vbCritical, "ADCX ERROR", , "ADCXGetEnt Function"

    End If

    End Function

    'funny little comparison function
    Public Function YoungerEntity(ByRef oEnt1 As AcadEntity, ByRef oEnt2 As
    AcadEntity) As AcadEntity
    If Hex2Dec(GetHandle(oEnt1)) > Hex2Dec(GetHandle(oEnt2)) Then
    Set YoungerEntity = oEnt1
    Else
    Set YoungerEntity = oEnt2
    End If

    End Function


    'simple wrapper
    Public Function GetHandle(oent As AcadEntity) As String
    GetHandle = oent.Handle
    End Function



    'Generously provided by: Juerg Menzi
    'Thanks Juerg!
    Public Function Hex2Dec(HexVal As String) As Variant

    Dim PosCnt As Integer
    Dim StrLen As Integer
    Dim TmpVal As Integer
    Dim CurDig As Variant
    Dim RetVal As Variant

    PosCnt = 1
    StrLen = Len(HexVal)

    Do
    CurDig = UCase(Mid(HexVal, PosCnt, 1))
    TmpVal = IIf(Asc(CurDig) > 64, Asc(CurDig) - 55, CVar(CurDig))
    RetVal = RetVal + (TmpVal * (16 ^ (StrLen - PosCnt)))
    PosCnt = PosCnt + 1
    Loop Until PosCnt > StrLen

    Hex2Dec = CDec(RetVal)

    End Function

    Hope some of that helps
    Mark
     
    Mark Propst, Jul 30, 2004
    #6
  7. Tom Roberts

    Mark Propst Guest

    Hi Laurie,
    Thanks for that explanation,
    I'm sure i've read about hex before but that's the clearest explanation I've
    seen.
    And even with that clarity, it took me a minute to figure out the 10=16 but
    then i see
    1 * 16 + 0
    cool

    Thanks,
    Mark
     
    Mark Propst, Jul 30, 2004
    #7
  8. Hi,

    Try this. A Hex number can be indicated by preceding it with &H

    Sub fred()
    Dim x As Long
    Dim y aAs Long
    y = &HABA
    x = 1135566
    If x > y Then
    MsgBox Hex(x) & " x>y " & Hex(y)
    Else
    MsgBox Hex(y) & " y >x " & Hex(x)
    End Sub

    --


    Laurie Comerford
    CADApps
    www.cadapps.com.au
     
    Laurie Comerford, Jul 30, 2004
    #8
  9. Tom Roberts

    Jürg Menzi Guest

    Hi Laurie

    Thanks for enlighten me...:cool:

    Didn't recognize the 'Hex' function in VB. My sample is a translation from
    LISP to VB. In this connection I got another maybe useful insight:
    - Handles are by type String
    - Hex needs a number and has limited range
    Solution:
    cDec("&H" + "FFFFFF")
    16777215

    Cheers
     
    Jürg Menzi, Jul 31, 2004
    #9
  10. Tom Roberts

    Jürg Menzi Guest

    Forget my unqualified babble...8-(

    'Hex' is the correct solution.

    Cheers
     
    Jürg Menzi, Jul 31, 2004
    #10
  11. Tom Roberts

    Jürg Menzi Guest

    Hi Mark

    Laurie is right. Hex is the more elegant way to compare handles.
    Therefore your solution can look like this (Hex2Dec -> garbage):

    'funny little comparison function
    Public Function YoungerEntity(ByRef oEnt1 As AcadEntity, _
    ByRef oEnt2 As AcadEntity) As AcadEntity
    If Hex(GetHandle(oEnt1)) > Hex(GetHandle(oEnt2)) Then
    Set YoungerEntity = oEnt1
    Else
    Set YoungerEntity = oEnt2
    End If

    End Function

    'simple wrapper
    Public Function GetHandle(oent As AcadEntity) As String
    GetHandle = "&H" + oent.Handle
    End Function

    Cheers
     
    Jürg Menzi, Jul 31, 2004
    #11
  12. Hi Tom, Mark and Jürg,

    Tom, thanks for the question and to the rest of us, this has been a great
    discussion and example of the symbiosis which can arise in the NGs.


    --


    Laurie Comerford
    CADApps
    www.cadapps.com.au


    <snip>
     
    Laurie Comerford, Aug 1, 2004
    #12
  13. Tom Roberts

    Tom Roberts Guest

    Wonderful stuff guys

    I pop up a question and go away for the week-end only to return to one of
    the more interesting threads I have read for a while, and a solution to my
    problem to boot.

    Thank-you

    --
    Regards
    Tom Roberts
    __________________________
    MechWest Design & Drafting
    Perth, Western Australia
     
    Tom Roberts, Aug 2, 2004
    #13
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.