[V8i SELECTseries 3 VBA] IncludeOnlyWithinRange

Good evening,

please see attached files, module: smartDim, sub: Dimension.

If I include IncludeOnlyWithinRange I get no element!

Why? 

Thanks a lot,

PaoloSketchesPrinting 2021.04.21 - WIP TO DIM.mvbaSLS180-62 .dgn

Parents
  • Hi Paolo,

    as Jon wrote: It would be better to extract code that does not work than to share complete project, because it makes analysis more complicated (and, in fact, it's not really safe to try to open and run 3rd party macro on own computer).

    Why? 

    Because there is a mistake: How Low limit can be bigger than High limit?

    myRange.Low.X = 0
    myRange.Low.Y = -1009 'wrong
    
    myRange.High.X = 2214
    myRange.High.Y = -2600 'wrong

    In fact, your code is written in a way "I want more such mistakes caused by unreadable code.". Things like defining all variables at the beginning are treated as wrong (it was was necessary more than 20 years ago in some languages, but today it's just a bad habit). Also, when a comment is required, it's probably a proof your code is dirty.

    Even when it's still not clean code ("do one thing only" principle is not followed), better is to (A) define variable exactly when is needed and (B) put all related code together:

    Sub Dimension()
    
    Dim myFilter As New ElementScanCriteria
    
    myFilter.ExcludeAllTypes
    myFilter.ExcludeAllColors
    myFilter.ExcludeAllLineStyles
    myFilter.ExcludeAllLineWeights
    
    myFilter.IncludeType msdElementTypeLine
    myFilter.IncludeType msdElementTypeLineString
    myFilter.IncludeType msdElementTypeArc
    
    myFilter.IncludeColor 0
    myFilter.IncludeLineStyle ActiveDesignFile.LineStyles("4")
    myFilter.IncludeLineWeight 0
    
    Dim myRange As Range3d
    myRange.High.X = 2214
    myRange.Low.X = 0
    myRange.High.Y = -1009 'Now it's clear it's wrong
    myRange.Low.Y = -2600  'because High.Y < Low.Y
    myFilter.IncludeOnlyWithinRange myRange
    
    Dim myEnum As ElementEnumerator
    Set myEnum = ActiveModelReference.Scan(myFilter)
    
    While myEnum.MoveNext
        Dim myAxeElement As Element
        Set myAxeElement = myEnum.Current
        MsgBox "Style: " & myAxeElement.LineStyle.Name & vbCrLf & _
         vbCrLf & "Weight: " & myAxeElement.LineWeight & vbCrLf & _
               "Color: " & myAxeElement.Color & vbCrLf & _
               "Cat: " & myAxeElement.Class
    Wend
    MsgBox "FINE"
    
    End Sub

    I think it's also better to use "4" as identification for Line style 4 than to use index value 5, because it's not clear when read that in fact, it's Line Style 4.

    Regards,

      Jan

Reply
  • Hi Paolo,

    as Jon wrote: It would be better to extract code that does not work than to share complete project, because it makes analysis more complicated (and, in fact, it's not really safe to try to open and run 3rd party macro on own computer).

    Why? 

    Because there is a mistake: How Low limit can be bigger than High limit?

    myRange.Low.X = 0
    myRange.Low.Y = -1009 'wrong
    
    myRange.High.X = 2214
    myRange.High.Y = -2600 'wrong

    In fact, your code is written in a way "I want more such mistakes caused by unreadable code.". Things like defining all variables at the beginning are treated as wrong (it was was necessary more than 20 years ago in some languages, but today it's just a bad habit). Also, when a comment is required, it's probably a proof your code is dirty.

    Even when it's still not clean code ("do one thing only" principle is not followed), better is to (A) define variable exactly when is needed and (B) put all related code together:

    Sub Dimension()
    
    Dim myFilter As New ElementScanCriteria
    
    myFilter.ExcludeAllTypes
    myFilter.ExcludeAllColors
    myFilter.ExcludeAllLineStyles
    myFilter.ExcludeAllLineWeights
    
    myFilter.IncludeType msdElementTypeLine
    myFilter.IncludeType msdElementTypeLineString
    myFilter.IncludeType msdElementTypeArc
    
    myFilter.IncludeColor 0
    myFilter.IncludeLineStyle ActiveDesignFile.LineStyles("4")
    myFilter.IncludeLineWeight 0
    
    Dim myRange As Range3d
    myRange.High.X = 2214
    myRange.Low.X = 0
    myRange.High.Y = -1009 'Now it's clear it's wrong
    myRange.Low.Y = -2600  'because High.Y < Low.Y
    myFilter.IncludeOnlyWithinRange myRange
    
    Dim myEnum As ElementEnumerator
    Set myEnum = ActiveModelReference.Scan(myFilter)
    
    While myEnum.MoveNext
        Dim myAxeElement As Element
        Set myAxeElement = myEnum.Current
        MsgBox "Style: " & myAxeElement.LineStyle.Name & vbCrLf & _
         vbCrLf & "Weight: " & myAxeElement.LineWeight & vbCrLf & _
               "Color: " & myAxeElement.Color & vbCrLf & _
               "Cat: " & myAxeElement.Class
    Wend
    MsgBox "FINE"
    
    End Sub

    I think it's also better to use "4" as identification for Line style 4 than to use index value 5, because it's not clear when read that in fact, it's Line Style 4.

    Regards,

      Jan

Children