DesignCAD 3D Max 2027 box
Now available

DesignCAD 2027

Hatches that follow their boundaries, an on-screen dial to turn your model, and a way to find any command by name, in a clean Windows 11 look.

Search Text

Started by bdeck, August 25, 2012, 08:46:23 PM

Previous topic - Next topic

0 Members and 0 Guests are viewing this topic.

bdeck

EDIT 8/28/12:  A non-case-sensitive version of this macro follows later in the thread.


'
'SearchText.d3m

' This is a simple improvement on the sample macro "TextSelect"
' It prompts the user for a text string, then searches through the drawing
' looking for every occurence of that string.  If a match is found, the matching piece of text is selected.
' It no matches are found, a message is displayed. Both 2D and 3D text strings are tested.
'
' NOTE:  The search is case sensitive, so the proper case must be used in the search string.  
'
' Deselect everything before starting
   Sys(80) = 0
'
' Un-remark these next 2 lines to make the input box appear in the upper
' left corner of the DesignCAD window, so it does not cover the drawing area.
   ' Sys(130) = 5
   ' Sys(131) = 5
' Get Text String from user
   Input "Enter Text to Search For . . .", TextStr$
'
' Find total number of entities
   count = sys(9)         
' Cycle through all entities
    found =0
   For i = 1 To count   
      Entity(i)   
      etype = Sys(90)   
      if (etype = 3) or (etype = 13)then  
         Cur$ = Sys$(1)
         if INSTR(Cur$,TextStr$)>0 then
            putattr i, etype, 1
            found=1
         endif
      endif
   next i


   if found=0 then
      Message "No Matching Text String Found in the drawing."
      End
   endif

   >Regenerate
   {
   }
   End

JJG

#1
Interesting code ! thanks !

My side, I also would be interested to know how many text string(s) are matching, in case of match : that's why I took the liberty to add 2-3 lines to your code (red-colored) :


'SearchText.d3m

' This is a simple improvement on the sample macro "TextSelect"
' It prompts the user for a text string, then searches through the drawing
' looking for every occurence of that string.  If a match is found, the matching piece of text is selected.
' It no matches are found, a message is displayed. Both 2D and 3D text strings are tested.
'
' NOTE:  The search is case sensitive, so the proper case must be used in the search string.  
'
' Deselect everything before starting
  Sys(80) = 0
'
' Un-remark these next 2 lines to make the input box appear in the upper
' left corner of the DesignCAD window, so it does not cover the drawing area.
  ' Sys(130) = 5
  ' Sys(131) = 5
' Get Text String from user
  Input "Enter Text to Search For . . .", TextStr$
'
' Find total number of entities
  count = sys(9)        
' Cycle through all entities
    found =0
  For i = 1 To count  
     Entity(i)  
     etype = Sys(90)  
     if (etype = 3) or (etype = 13)then  
        Cur$ = Sys$(1)
        if INSTR(Cur$,TextStr$)>0 then
           putattr i, etype, 1
           found=found+1
        endif
     endif
  next i


  if found=0 then
     Message "No Matching Text String Found in the drawing."
     End
  endif

  if found>0 then
   Precision 0
   found$ = found
      if found = 1 then
              mes$ = found$ + "  Matching Text String Found in the drawing."
              Message mes$
         else
               mes$ = found$ + "  Matching Text Strings Found in the drawing."
              Message mes$
         endif
   endif


  >Regenerate
  {
  }
  End


JJG

Another utility of this code, except to find some parts of string, if the same kind of work is done on attributes, for example, could be the counting of subsets of parts : DesignCAD knows how to find things by direct counting different attributes (ie material list, found in Menu File / Material List), but not subsets of attributes.
For example, imagine that some parts have attributes "type of screw + dimensions of the screw" ;
Then, using this kind of macro, you can have directly the count of thing you are searching for, eg all screws of "diameter xx" or all screw of "length yy", searching for these kind of thing throught the entire drawing ...
Well, that gives me ideas ... !

Thank you very much, Bdeck!
;)

bdeck

#3
Hi JJG, here's another version...

'
' SearchText3.d3m        NOT CASE-SENSITIVE
'
' This is a non-case-sensitive improvement on the sample macro "TextSelect"
' It prompts the user for a text string, then searches through the drawing
' looking for every occurence of that string.  If a match is found, the matching text is selected.
' If not, a message is displayed. Both 2D and 3D text strings are tested.
'
' This version adds optional output and early exit from main loop
'
' Deselect everything before starting
   Sys(80) = 0
'
' Un-remark these next 2 lines to make the input box appear in the upper
' left corner of the DesignCAD window, so it does not cover the drawing area.
   ' Sys(130) = 5
   ' Sys(131) = 5
' Get Text String from user
   Input "Enter Text to Search For . . .", TextStr$

' Find total number of entities
   count = sys(9)         
   tlen=len(textstr$)
' Cycle through all entities
     found =0
   For i = 1 To count
      tf=1
      ts$=textstr$
      Entity(i)   
      etype = Sys(90)   
      if (etype = 3) or (etype = 13)then 
         Cur$ = Sys$(1)
         for j=1 to tlen
            gosub getleft
            if INSTR(Cur$,tstr$)=0 then gosub swapit
            if tf=0 then
               j=tlen
            else
               if j=tlen then
               putattr i, etype, 1
               found=found+1
               endif
            endif
         next   
         
      endif
   next i


   if found=0 then
      Message "No Matching Text String Found in the drawing."
      End
   endif
   
   >Regenerate
   {
   }
' Un-remark these next 2 lines to make result appear in status bar
   'print found, " strings found"
   'anykey
   End


getleft:
   do while (mid$(ts$,j,1) < "A") and (j<tlen)
      j=j+1
   loop   
   tstr$=left$(ts$,j)
return

swapit:
   dif = asc("a")- asc("A")
   a=asc(MID$(tstr$,j,1))   
   if a>64 then
      if a< asc("a") then
         a= a+dif
      else
         a=a-dif
      endif
      a$=chr$(a)
      ts$=left$(ts$,j-1)+a$+right$(ts$,tlen-j)
      tstr$= left$(ts$,j)
      if (INSTR(Cur$,tstr$)=0) then tf=0 'text not found
   else
      tf=0
   endif
return
   

   

JJG

Interesting too !  THANKS !
;)