News:

Please note new Forum URL: forum.designcad.com

Main Menu
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.

STL - File Import

Started by ablincoe, March 29, 2014, 07:15:19 PM

Previous topic - Next topic

0 Members and 0 Guests are viewing this topic.

ablincoe

I noticed a few people asking about such a thing on the forum, so I thought I would give it a whirl.  The  STL-ASCII  was a piece of cake, but the STL-BINARY had a number of challenges.  If you REALLY LOVE triangles, give this thing a try.  It does not attempt to guess at the underlying structure, and does not do any type of checking for holes (or is the wholes) or overlapping geometry.

Large BINARY files will take a  -  LONG TIME  -  to get imported.  I guestimated an 8Mb file with just over 110,000 triangles would take 18-20 hours.  I wasn't that patient!

Allen

P.S.:  The other tasks (Geometry check (holes, overlaps), export, etc.) look to be beyond what BasicCAD and the available entity information are capable of.

Dr PR

Allen,

I have a few STL files that were generated with AccuTrans 3D. When I get time I will test the macro.

Phil
DesignCAD user since 1987

Bob P

I changed the macro a bit to generate planes rather than just triangular vector entities.  I also moved the log file to d:\temp. (Line 81) Change the location to your preference.

It seems to work for me.

'  STL (3D-Printer) file read/import
'  Usage:
' Go to an - UNUSED - layer on your drawing
' SET the color you want to be used for the lines
' Atrbute Bytes are NOT supported
' Run the Macro  (STL.d3m)
' GIVE IT SOME TIME TO WORK
'    It will end with a message giving the "Triangle Count"
' ASCII Processes about  750 per minute
' BINARY processes about 700 per minute
'   (File size in Bytes/50  will give triangle count)
' As the number of triangles gets larger, the process slows down
' First  10,000 (from BINARY) takes about  -  15 minutes
' Second 10,000 takes about  -  25 minutes
' Third  10,000 takes about  -  40 minutes
' Fourth 10,000 takes about  -  55 minutes
' Fifth  10,000 takes about  -  65 minutes
' Sixth  10,000 takes about  -  75 minutes
' This process - DOES NOT - try to guess at underlying geometry
' This process - DOES NOT - check for holes or over-laps
'
'  Description:
' This may be the implementation of a  -  SUPER-SET
' of the  -  ASCII - STL  -  File definition
' ALL checked TEXT areas are converted  -  TO - LOWER Case
'     Just in case someone misspells something
'
'  SUB-ROUTINES Utilized
' STL_FILE: Determine what type of "STL" file this is
' Branches to  -  STL_ASCII  or  STL_BINARY
' STL_ASCII Process an ASCII STL file into the current layer
' Does about -
' STL_BINARY Process a  BINARY STL file into the current layer
' GET_REC A method to read a binary file a known number of BYTES
' at a time.
' SPLIT_WHITE A method to split a string of text into words  (A$)
' TO_LOWER A method to insure that all text is in LOWER case (A$)
' for easier comparison  -  Returns  M_A$
' UINT_TO_NUM A conversion of Byte-Binary - Unsigned Integer
'   to a  BasicCAD  #
'   IF - Little ENDIAN is needed,  Set the variable
' ENDIAN  to  < 0  (-1)
'   Handles any size - U-INT - passe to it (in A$)
'   Returns number in  -  M_NUM
'
' REAL_TO_NUM A conversion of Byte-REAL(8, 16, 32 Bit) to a Number
'   Utilizes  -  UINT_TO_NUM - for 8 & 16-Bit REALs
' CVS() - for 32-Bit REALs
' (may need LITTLE ENDIAN Adjust)
'   Returns the number in  -  M_NUM
' DUMP_REC A  HEX & ASCII  Print of a passed string of characters
'   Utilizes my standard log file  (print #2)

DIM SPLIT$(100) '  For - SPLIT_WHITE  -  SubRoutine

  INPUT  "Enter the (STL) File Name: ", FL_NAME$ '  Get FILE Name
  FL_TYPE$ = "STL" '  SET File Type
    A$ = FL_TYPE$ '    To Standard Pass $ Area
    GoSub TO_LOWER '      Force TO - LOWER - Case
  IF M_A$ = "stl"  THEN GoSub  STL_FILE '  FILE name was  -  *.stl

THE_END:
  SYS(36) = 0
  >RegenerateAll
  {
  }
  REGEN
  print #2, "Ending at:    ", SYS$(8), "   ", SYS$(7)
  print #2, "Triangles Processed:  ", TRI_CNT
  Message TRI_CNT, "  -  Triangles Drawn" + CHR$(13) + _
  "Start/End:  ", ST_DATE$ + "  " + ST_TIME$ +  _
  "    " + SYS$(8) + "   " + SYS$(7)
  CLOSE 1
  CLOSE 2
END


STL_FILE:
  OPEN "A", 2, "d:\temp\STL.log" '  A LOG FILE PLACE
  OPEN "I", 1, FL_NAME$ '  Open File
  INPUT #1, A$ '    READ FIRST data line - ASCII/BIN
    Z$ = A$ ' SAVE off just in case
    A$ = MID$(A$,1,5) ' What do I want converted
    GoSub TO_LOWER ' Force String lower Case
  print #2, "Starting at:  ", SYS$(8), "   ", SYS$(7)
ST_DATE$ = SYS$(8)
ST_TIME$ = SYS$(7)
  IF M_A$ = "solid"  THEN '    Is it an - ASCII - STL File - ??
print #2, "  STL ASCII data file:  ", FL_NAME$
GoSub STL_ASCII ' Bring in the ASCII Triangles
    ELSE '      BINARY - STL File
ENDIAN = -1 '  Numbers - LITTLE ENDIAN
print #2, "  STL BINARY data file:  ", FL_NAME$
GoSub STL_BINARY
    END IF
RETURN

STL_ASCII: '  ASCII STL File - Do Triangles
  SYS(36) = 2 '    EXTRA Quiet mode
  TRI_CNT = 0
  DO WHILE  NOT  EOF(1) '    READ data file until  EOF
    IF (TRI_CNT MOD 100) = 0   THEN ' Time to REFRESH the Scrn
SYS(36) = 0
>RegenerateAll
{
}
REGEN
SYS(36) = 2
      END IF
    INPUT #1, A$ ' READ next data line
    Z$ = A$ ' SAVE off just in case
    GoSub SPLIT_WHITE ' SPLIT line into words
    A$ = SPLIT$(1) + " " + SPLIT$(2)
    GoSub TO_LOWER '   Insure  lower  case
    IF  LEFT$(M_A$,8) = "endsolid"  THEN '  Found - THE END
RETURN '       Return back to caller
END IF
    IF  M_A$ = "facet normal"  THEN ' HERE - is a Triangle start
      TRI_CNT = TRI_CNT + 1
      INPUT #1, A$ '     Bypass - "outer loop"
      INPUT #1, A$ '     Get "vertex" - 1
Z$ = A$ ' SAVE off just in case
GoSub SPLIT_WHITE '     Split into words
IF M_VAR = 4  THEN
  X1$ = (SPLIT$(2)) ' Get Point X1
  Y1$ = (SPLIT$(3)) ' Get Point Y1
  Z1$ = (SPLIT$(4)) ' Get Point Z1
  END IF
      INPUT #1, A$ '     Get "vertex" - 2
Z$ = A$ ' SAVE off just in case
GoSub SPLIT_WHITE '     Split into words
IF M_VAR = 4  THEN
  X2$ = (SPLIT$(2)) ' Get Point X2
  Y2$ = (SPLIT$(3)) ' Get Point Y2
  Z2$ = (SPLIT$(4)) ' Get Point Z2
  END IF
      INPUT #1, A$ '     Get "vertex" - 3
Z$ = A$ ' SAVE off just in case
GoSub SPLIT_WHITE '     Split into words
IF M_VAR = 4  THEN
  X3$ = (SPLIT$(2)) ' Get Point X3
  Y3$ = (SPLIT$(3)) ' Get Point Y3
  Z3$ = (SPLIT$(4)) ' Get Point Z3
  END IF
      INPUT #1, A$ '     Get "endloop"
      INPUT #1, A$ '     Get "endfacet"
      X1 = VAL(X1$)
      Y1 = VAL(Y1$)
      Z1 = VAL(Z1$)
      X2 = VAL(X2$)
      Y2 = VAL(Y2$)
      Z2 = VAL(Z2$)
      X3 = VAL(X3$)
      Y3 = VAL(Y3$)
      Z3 = VAL(Z3$)
      >Plane '   DRAW the TRIANGLE
       {
<PointXYZ [X1, Y1, Z1] ' Stasrting Point
<PointXYZ [X2, Y2, Z2] '   Via Point 2
<PointXYZ [X3, Y3, Z3] '   Via Point 3
' <PointXYZ [X1, Y1, Z1] '   Return to Point 1
       }
    END IF
    LOOP
RETURN

STL_BINARY: '  STL - BINARY - data file
  SYS(36) = 2 '    EXTRA - Silent
  CLOSE 1 '    CLOSE the (I)nput File
  OPEN "R", 1, FL_NAME$, 1 '    OPEN  the (R)andom (BIN) Fl
FL_BYTE = 0

  REC_LEN = 80 '    HEADER Length
    GoSub GET_REC ' Read it
      HDR$ = ""
      FOR i = 1 to LEN(M_A$)
IF MID$(M_A$,i,1) >= " "  THEN
  HDR$ = HDR$ + MID$(M_A$,i,1)
  END IF
NEXT I
    print #2, "  Header: ", HDR$ ' LOG Header data
  REC_LEN = 4 '    TRIANGLE Count
    GoSub GET_REC ' Read it
      A$ = M_A$
      GoSub UINT_TO_NUM ' Unsigned Integer Conv
      TRI_CNT = M_NUM ' Get TRIANGLE Count
      print #2, "  Triangle Count: ", TRI_CNT

  REC_LEN = 50 '    TRIANGLE DEFINITION Length
  FOR P_TRI = 1 to TRI_CNT
    IF (P_TRI MOD 150) = 0  THEN ' PERIODIC LOGGING
      print #2, "    Mid Count at: ", SYS$(8), "   ", SYS$(7), "  ", P_TRI
      END IF
    IF (P_TRI MOD 1000) = 0   THEN ' Time to REFRESH the Scrn
      REGEN
      END IF
    GoSub GET_REC ' Read next Triangle
      TRI$ = M_A$ '   Be Sure to Keep it
' SKIPPING Outside Pntr
      A$ = MID$(TRI$,13,4) '   TRI - X1
GoSub REAL_TO_NUM ' Conv to Number
X1 = M_NUM
      A$ = MID$(TRI$,17,4) '   TRI - Y1
GoSub REAL_TO_NUM ' Conv to Number
Y1 = M_NUM
      A$ = MID$(TRI$,21,4) '   TRI - Z1
GoSub REAL_TO_NUM ' Conv to Number
Z1 = M_NUM
      A$ = MID$(TRI$,25,4) '   TRI - X2
GoSub REAL_TO_NUM ' Conv to Number
X2 = M_NUM
      A$ = MID$(TRI$,29,4) '   TRI - Y2
GoSub REAL_TO_NUM ' Conv to Number
Y2 = M_NUM
      A$ = MID$(TRI$,33,4) '   TRI - Z2
GoSub REAL_TO_NUM ' Conv to Number
Z2 = M_NUM
      A$ = MID$(TRI$,37,4) '   TRI - X3
GoSub REAL_TO_NUM ' Conv to Number
X3 = M_NUM
      A$ = MID$(TRI$,41,4) '   TRI - Y3
GoSub REAL_TO_NUM ' Conv to Number
Y3 = M_NUM
      A$ = MID$(TRI$,45,4) '   TRI - Z3
GoSub REAL_TO_NUM ' Conv to Number
Z3 = M_NUM

    >Plane '    DRAW the plane
      {
       <PointXYZ [X1, Y1, Z1] ' Starting Point
       <PointXYZ [X2, Y2, Z2] '   Via Point 2
       <PointXYZ [X3, Y3, Z3] '   Via Point 3
       <PointXYZ [X1, Y1, Z1] '   Return to Point 1
      }
   
    NEXT P_TRI
RETURN

GET_REC: '  Entering - GET_REC
  IF REC_LEN < 1  THEN ' Is a Record Len defined
print #2, "BAD RECORD LENGTH PASSED - to GET_REC: ", REC_LEN
GoTo THE_END '   B_A_I_L
END IF
  M_A$ = "" '    CLEAR return area
  for M_I = 1 to REC_LEN '    For each byte ...
    GET 1, (FL_BYTE + M_I), A$ ' GET the next one
    IF EOF(1)  THEN
M_I = REC_LEN + 1
M_A$ = ""
END IF
    M_A$ = M_A$ + A$ ' Accumulate it in RET
    NEXT M_I
  IF LEN(M_A$) <> REC_LEN  THEN '    Is RETURN area proper size
print #2, "BAD RECORD READ - in GET_REC: ", REC_LEN, LEN(M_A$)
GoTo THE_END ' B_A_I_L
END IF
  FL_BYTE = FL_BYTE + REC_LEN '    LAST File Byte Read
RETURN

SPLIT_WHITE:
  FOR M_IDX = 1 to 100 '  CLEAR DESTINATIONs
    SPLIT$(M_IDX) = "" '   One at a time
    NEXT I
  M_IDX = 1 '  Where to start within  -  A$
  M_L   = LEN(A$) '  Where must I stop
  M_VAR = 1 '  Index for Array Offset

  DO WHILE  M_IDX <= M_L '  START THE PARSING
    M_CH$ = MID$(A$,M_IDX,1) '    My (next) Character
    IF  SPLIT$(M_VAR) <> ""  AND  _ '  TIME for the NEXT entry ?
   M_CH$ <= " "  THEN
      M_VAR = M_VAR + 1 '    INCREMENT Index Pointer
      IF M_VAR = 100  THEN '    LAST VARIABLE available
   SPLIT$(M_VAR) = MID$(A$,M_IDX,9999) '   SAVE THE REST here
   M_CH = " " '   Clear CHAR Buffer
   M_IDX = M_L '   Set END OF INPUT
   END IF
      END IF
    IF  M_CH$ > " "  THEN '  A character needs saving
      SPLIT$(M_VAR) = SPLIT$(M_VAR) + M_CH$ '    SAVE the character at IDX
      END IF
    M_IDX = M_IDX + 1 '    Move on down the string
    LOOP
RETURN

TO_LOWER: '  INSURE STRING IS
  M_IDX = 1 '    lower - case
  M_L   = LEN(A$) ' String length
  M_A$ = "" '    Converted $
  DO WHILE  M_IDX  <=  M_L '    FOR EACH CHARACTER
    M_X = ASC(MID$(A$,M_IDX,1)) ' Get it's ASCII
    IF M_X > 64  AND M_X < 96  THEN ' IF - UPPER
M_X = M_X + 32 '   MAKE lower
END IF
    M_A$ = M_A$ + CHR$(M_X) '    BUILD output $
    M_IDX = M_IDX + 1
    LOOP
RETURN


' ==================================================================

UINT_TO_NUM: '  UNSIGNED INTEGER -> Number
'  RETURNS  -  M_NUM
  IF  ENDIAN < 0  THEN '  LITTLE ENDIAN on input ! ! !
    M_B$ = "" ' CLEAR Destination
    for M_I = LEN(A$) to 1  step -1 ' START Converting it
M_B$ = M_B$ + MID$(A$,M_I,1) '   One Byte at a time
NEXT M_I '   Until done
    A$ = M_B$ '  Set back into input
    END IF
  M_X = 0 '  CLEAR Accumulator
  M_BYTES = LEN(A$) '  How many bytes passed in  ?
  M_BITS = M_BYTES * 8 '    Get Number of bits
  FOR  M_I = 1 to LEN(A$) '  For EACH Input Byte
    M_X = M_X * 2^8 '    Bump Value for next Byte
    IF  MID$(A$,M_I,1) > CHR$(0)  THEN '    Non - Zero ?
M_X = M_X + ASC(MID$(A$,M_I,1)) '    Add next BYTE value
END IF
    NEXT M_I
  M_NUM = M_X '    SET Return Value
' Message "UINT -> Number:  ", LEN(A$), ",  ", M_NUM
RETURN '    RETURN from - UINT_TO_NUM

REAL_TO_NUM: '  CHAR_FLOAT -> Number
' IF  ENDIAN < 0  THEN '  LITTLE ENDIAN on input ! ! !
' ' HANDLED IN - UINT_TO_NUM

  IF LEN(A$) =  8  THEN '  SYSTEM STANDARD - DOUBLE  ?
    M_NUM = CVS(A$) '    YES - SYSTEM - CVS(A$)
    RETURN ' RETURN to caller
    END IF
  GoSub UINT_TO_NUM '    Go get the whole thing
  IF LEN(A$) =  4  THEN '  SINGLE - FLOAT -> Number
    M_SGN_CNT = 1 '    SIGN Bits
    M_EXP_CNT = 8 '    EXPONENT Bits
    M_SIG_CNT = 23 '    SIGNIFICAND Bits
    M_EXP_BIAS = 127 '    Exponent Bias
    END IF
  IF LEN(A$) =  2  THEN '  HALF - FLOAT -> Number
    M_SGN_CNT = 1 '    SIGN Bits
    M_EXP_CNT = 5 '    EXPONENT Bits
    M_SIG_CNT = 10 '    SIGNIFICAND Bits
    M_EXP_BIAS = 15 '    Exponent Bias
    END IF
  M_X = M_SGN_CNT + M_EXP_CNT + M_SIG_CNT
  IF  M_X <> M_BITS  THEN
SYS(36) = 0
Message "M_BITS  Calculation Error: ", M_X, ",  ", M_BITS
END IF
  M_BITS = M_BITS - 1

  M_SGN = 1 '  CLEAR/SET Work Areas
  M_EXP = 0
  M_SIG = 0
  M_X = M_NUM
'  SIGN Bit Handling
  IF  M_X >= 2^M_BITS  THEN ' Bit On  ?
M_SGN = -1 '   SET Sign Bit
M_X = M_X - 2^M_BITS
END IF
  M_EXP =  INT(M_X / 2^(M_SIG_CNT)) '  EXPONENT Handling
    EXP_CK = M_EXP
    M_EXP = M_EXP - M_EXP_BIAS '    BIAS offset
  M_SIG =  M_X MOD 2^M_SIG_CNT '  SIGNIFICAND - split off bits
    SIG_CK = M_SIG
  M_X   =  M_SIG '    SAVE only it for working
  M_Y   =  M_SIG_CNT - 1 '    Number of BITs to work with
  M_SIG = 0 '    CLEAR result
    for  M_I = (M_SIG_CNT - 1) to 0  Step -1 '    SIGNIFICANT Bits Handling
      IF M_X >= 2^M_I  THEN '    Bit On  ?
M_SIG = M_SIG + 2^(0 - (M_Y - M_I) - 1) ' INC - Signif
M_X = M_X - 2^M_I ' Adjust  M_X
END IF
      NEXT M_I
    M_SIG = M_SIG + 1 '    ADD the implied "1.0"
  M_NUM = M_SGN * 2^M_EXP * M_SIG '  THE NUMBER to Return
'
'  NON-REAL NUMBER  -  Check
  IF  (EXP_CK = (2^M_EXP_CNT) - 1)  AND _ '  EXP - ALL BITS ON - NON-REAL
((SIG_CK = 0)  OR _ ' Infinity
(SIG_CK = ((2^M_SIG_CNT) - 1)))  THEN ' Not-a-Number
SYS(36) = 0
Message "SPECIAL CASE NUMBER located", _
CHR$(13), "INFINITY  or  ""NaN"""
M_NUM = 0 ' CLEAR RETURN Value
    END IF
RETURN


DUMP_REC: '  Entering - DUMP_IT
  PRECISION 0
  HEX$ = "0123456789ABCDEF" '    A "Hex Dump" of what was
  D_REC = D_REC + 1 '    Read in
  print #2, D_REC, " - |";
  for i = 1 to LEN(A$) '    FOR Each Byte ...
    IF MID$(A$,i,1) > CHR$(0)  THEN
      w = ASC(MID$(A$,i,1)) ' Get it's value
     ELSE
      w = 0
      END IF
    x = INT(w/16) + 1 ' Upper Nibble area
    y = w MOD 16 + 1 ' Lower Nibble area
    PRT$ = MID$(HEX$,x,1) + MID$(HEX$,y,1)
    print #2, PRT$ + " "; ' Print it
    IF W >= 20  AND w <=128  THEN ' If Printable
PRT_2$ = PRT_2$ + MID$(A$,i,1) '     Store it for later
      ELSE
PRT_2$ = PRT_2$ + "." '     or store a period
      END IF
    NEXT i
  IF LEN(PRT_2$) > 10  THEN print #2, "" ' Put new line out
  print #2, "|-----|", PRT_2$ ' Print printable values
PRT_2$ = "" '   CLEAR printable vals
RETURN

prl

Thanks Allen,  I like reading the subroutines.  I changed the name to STLIN.d3m.   Maybe the IMSI/Design programming gurus will give us the STLOUT in the future.

prl

For large projects, had you considered that most of the parsing and processing could be done by a compiled program which then generates a simple x,y,z coordinate data file that DesignCAD reads and simply draws planes or triangles?  In other words, the macro calls an *.exe file that crunches the raw STL data and prepares a xyz planes.dat file or something like that and then the macro loops and reads those three points and draws planes.  I'm guessing that would be pretty fast.


prl

I did an informal timing test on a 2006 XP Dell I9400 laptop Intel Core2 CPUT7200 @ 2.00 GHZ.

Using basiccad to read a x,y,z data file,  DesignCAD can read and draw 225 triangular planes/sec (suppressing screen regens etc.) or equivalently about 13,500 planes/min.  On that machine, a file with 100,000 planes would take about 7.5 minutes.




ablincoe

PRL,
I think it's been around 20 years since I've worked in a "compiled" language, and that was COBOL.  Normally, the basic tools provided with the systems I worked with were more than fast enough to get the job done.  Maybe this will serve as an inspiration to our more modern workers on what can be done.  A Windows VB/J-Script may work quite will to do the file reading and Type-Cast the numbers into something appropriate much quicker than my brute force methods.

prl

#7
Quote from: ablincoe on March 30, 2014, 07:04:28 AM
I think it's been around 20 years since I've worked in a "compiled" language, and that was COBOL.

Understood.  I'm of an age where I'm trying to hang onto things that I once knew.  My last compiler (and still is) 32 bit Borland Delphi 7 (2002).  Never say never, but that's probably it for me.  I've done the "Hello World" too often.

Dean

Quote from: Bob P on March 30, 2014, 05:14:49 AM
I changed the macro a bit to generate planes rather than just triangular vector entities.  I also moved the log file to d:\temp. (Line 81) Change the location to your preference.

It seems to work for me.


Hey, Thanks Bob! That worked great. I exported an stl file from blender. I tried your script in V18 and V23, (see attachment) they both took the same amount of time with 5023 triangles though I liked the way V18 was handling the graphics, much better than V23.  I attached the stl file as a txt file just change the extension to stl,if you want to test it.
DesignCad 3D max 2021
Lenovo 17.3"Laptop Intel(R) Core(TM) i7-9750H CPU @ 2.6GHz/Turbo boost 6 cores 12 threads.16GB memory.SSD 256GB/HDD 1TB 7200 Rpm.Nvidia Graphics GeForce GTX 1660 Ti 6GB
Windows 10 Home 64 bit

Bob P

I can't take any credit for the basic script.  I only changed 2 or 3 lines in it to make planes instead of closed vectors.  Not a big deal at all if you understand BasicCad.

ablincoe

Here is a SIGNIFICANT improvement to the STL import.
-  The drawing of the object during processing is now "optional".  I would suggest not using the macro to draw the object.
-  It can now create an XYZ data file to be read in by the normal DC import process (which is TONS faster than the macro drawing the triangles).  The XYZ data file uses the same STL Object Name with a ".xyz" extension on it.
-  There is somewhat of a check for potential holes in the object (it IS NOT perfect, but...)
      It looks at how many connections a single point has, and if <= 3, will log the point as a potential trouble spot.
          1 = a VERY definite problem  (a point not connected to any other triangle)
          2 = a MOST PROBABLE problem  (a point connected to only 1 other triangle)
          3 = Most likely not a problem, but it may be worth checking.

If you TURN OFF the building of the object in the macro, a 200,000 facet (8.5Mb binary file) can be processed in about 1 hour total.  You will need to manually import the XYZ data build.

'  STL (3D-Printer) file read/import
'  Usage:
' Go to an - UNUSED - layer on your drawing
' SET the color you want to be used for the lines
' Atribute Bytes are NOT supported
' Run the Macro  (STL.d3m)
' GIVE IT SOME TIME TO WORK
'    It will end with a message giving the "Triangle Count"
' ASCII Processes about  750 per minute
' BINARY processes about 700 per minute
'   (File size in Bytes/50  will give triangle count)
' As the number of triangles gets larger, the process slows down
' First  10,000 (from BINARY) takes about  -  15 minutes
' Second 10,000 takes about  -  25 minutes
' Third  10,000 takes about  -  40 minutes
' Fourth 10,000 takes about  -  55 minutes
' Fifth  10,000 takes about  -  65 minutes
' Sixth  10,000 takes about  -  75 minutes
' This process - DOES NOT - try to guess at underlying geometry
' This process - DOES NOT - check for holes or over-laps
'
'  Description:
' This may be the implementation of a  -  SUPER-SET
' of the  -  ASCII - STL  -  File definition
' ALL checked TEXT areas are converted  -  TO - LOWER Case
'     Just in case someone misspells something
'
'  SUB-ROUTINES Utilized
' STL_FILE: Determine what type of "STL" file this is
' Branches to  -  STL_ASCII  or  STL_BINARY
' STL_ASCII Process an ASCII STL file into the current layer
' Does about -
' STL_BINARY Process a  BINARY STL file into the current layer
' GET_REC A method to read a binary file a known number of BYTES
' at a time.
' SPLIT_WHITE A method to split a string of text into words  (A$)
' TO_LOWER A method to insure that all text is in LOWER case (A$)
' for easier comparison  -  Returns  M_A$
' UINT_TO_NUM A conversion of Byte-Binary - Unsigned Integer
'   to a  BasicCAD  #
'   IF - Little ENDIAN is needed,  Set the variable
' ENDIAN  to  < 0  (-1)
'   Handles any size - U-INT - passe to it (in A$)
'   Returns number in  -  M_NUM
'
' REAL_TO_NUM A conversion of Byte-REAL(8, 16, 32 Bit) to a Number
'   Utilizes  -  UINT_TO_NUM - for 8 & 16-Bit REALs
' CVS() - for 32-Bit REALs
' (may need LITTLE ENDIAN Adjust)
'   Returns the number in  -  M_NUM
' DUMP_REC A  HEX & ASCII  Print of a passed string of characters
'   Utilizes my standard log file  (print #2)

DIM SPLIT$(100) '  For - SPLIT_WHITE  -  SubRoutine

  INPUT  "Enter the (STL) File Name: ", FL_NAME$ '  Get FILE Name
  FL_TYPE$ = "STL" '  SET File Type
    A$ = FL_TYPE$ '    To Standard Pass $ Area
    GoSub TO_LOWER '      Force TO - LOWER - Case
  IF M_A$ = "stl"  THEN GoSub  STL_FILE '  FILE name was  -  *.stl

THE_END:
  print #2, "Ending at:    ", SYS$(8), "   ", SYS$(7)
  print #2, "Triangles Processed:  ", TRI_CNT
  Message TRI_CNT, "  -  Triangles Drawn" + CHR$(13) + _
  "Start/End:  ", ST_DATE$ + "  " + ST_TIME$ +  _
  "    " + SYS$(8) + "   " + SYS$(7)
  CLOSE 2
END


STL_FILE:
  INPUT "Create Object (1), or Create XYZ (2), Both (3):  ", CR_OBJ
  INPUT "Log Analysis (NO=0, YES=1):  ", ANALYZE

  Z_FL$ = MID$(FL_NAME$,1,LEN(FL_NAME$) - 4)
  IF CR_OBJ <> 1  THEN '  Create XYZ instead of object(s)
    XYZ_FL$ = Z_FL$ + "_XYZ.xyz" ' NAME of the XYZ file
    OPEN "O", 3, XYZ_FL$ '   Open it
    END IF
  IF ANALYZE = 1  THEN
    W_FL$ = Z_FL$ + "_WRK.txt"
    S_FL$ = Z_FL$ + "_SRT.txt"
    OPEN "O", 4, W_FL$
    END IF

  OPEN "A", 2, "C:\tmp\STL.log" '  A LOG FILE PLACE
  OPEN "I", 1, FL_NAME$ '  Open File
  INPUT #1, A$ '    READ FIRST data line - ASCII/BIN
    Z$ = A$ ' SAVE off just in case
    A$ = MID$(A$,1,5) ' What do I want converted
    GoSub TO_LOWER ' Force String lower Case
  print #2, "Starting at:  ", SYS$(8), "   ", SYS$(7)
ST_DATE$ = SYS$(8)
ST_TIME$ = SYS$(7)
  IF M_A$ = "solid"  THEN '    Is it an - ASCII - STL File - ??
print #2, "  STL ASCII data file:  ", FL_NAME$
GoSub STL_ASCII ' Bring in the ASCII Triangles
    ELSE '      BINARY - STL File
ENDIAN = -1 '  Numbers - LITTLE ENDIAN
print #2, "  STL BINARY data file:  ", FL_NAME$
GoSub STL_BINARY
    END IF
  CLOSE 1
  SYS(36) = 0
  >RegenerateAll
  {
  }
  REGEN

  IF CR_OBJ <> 1  THEN '  Close a created XYZ data file ?
    CLOSE 3
    print #2, "XYZ data file built as:  ", XYZ_FL$
    END IF
  IF ANALYZE = 1  THEN '  ANALYZE point usage for issues ?
    CLOSE 4
    print #2, "Sorting Point Usage data."
    A$ = "cmd.exe  /c Sort  " + W_FL$ + "   > " + S_FL$
    RUN  A$, 1
    print #2, "    Data sorted."
    PT_CNT = 0
    PT_USE = 0
    PT$ = ""
    Open "I", 1, S_FL$
    print #2, "Problem Area(s) Noted.  ", SYS$(8), "   ", SYS$(7)
    DO WHILE  NOT  EOF(1)
      INPUT #1, A$
      if  PT$ <> A$  AND  PT$ <> ""  THEN '  Got to next point
if PT_USE <= 3  THEN
print #2, "    Usage, Point:  ", PT_USE + ", ", PT$
END IF
PT$ = "" ' CLEAR WORKING POINT
PT_USE = 0 ' CLEAR POINT USAGE COUNT
END IF
      if PT$ = ""  THEN '  NEW working point ?
PT$ = A$ ' Set working value
PT_CNT = PT_CNT + 1 ' # Points analyzed
END IF
      PT_USE = PT_USE + 1 '  Individual point usage count
      LOOP
    CLOSE 1   
    print #2, "  End of Point Analysis Issue List:  ", PT_CNT
    INPUT "Remove Analysis Work files (0=NO, 1=YES)?  ", REMOVE
    if REMOVE = 1  THEN
A$ = "cmd.com  /c del " + W_FL$
RUN  A$, 1
A$ = "cmd.com  /c del " + S_FL$
RUN  A$, 1
END IF
    END IF
RETURN

STL_ASCII: '  ASCII STL File - Do Triangles
  SYS(36) = 2 '    EXTRA Quiet mode
  TRI_CNT = 0
  DO WHILE  NOT  EOF(1) '    READ data file until  EOF
    IF (TRI_CNT MOD 100) = 0   THEN ' Time to REFRESH the Scrn
SYS(36) = 0
>RegenerateAll
{
}
REGEN
SYS(36) = 2
      END IF
    INPUT #1, A$ ' READ next data line
    Z$ = A$ ' SAVE off just in case
    GoSub SPLIT_WHITE ' SPLIT line into words
    A$ = SPLIT$(1) + " " + SPLIT$(2)
    GoSub TO_LOWER '   Insure  lower  case
    IF  LEFT$(M_A$,8) = "endsolid"  THEN '  Found - THE END
RETURN '       Return back to caller
END IF
    IF  M_A$ = "facet normal"  THEN ' HERE - is a Triangle start
      TRI_CNT = TRI_CNT + 1
      INPUT #1, A$ '     Bypass - "outer loop"
      INPUT #1, A$ '     Get "vertex" - 1
Z$ = A$ ' SAVE off just in case
GoSub SPLIT_WHITE '     Split into words
IF M_VAR = 4  THEN
  X1$ = (SPLIT$(2)) ' Get Point X1
  Y1$ = (SPLIT$(3)) ' Get Point Y1
  Z1$ = (SPLIT$(4)) ' Get Point Z1
  END IF
      INPUT #1, A$ '     Get "vertex" - 2
Z$ = A$ ' SAVE off just in case
GoSub SPLIT_WHITE '     Split into words
IF M_VAR = 4  THEN
  X2$ = (SPLIT$(2)) ' Get Point X2
  Y2$ = (SPLIT$(3)) ' Get Point Y2
  Z2$ = (SPLIT$(4)) ' Get Point Z2
  END IF
      INPUT #1, A$ '     Get "vertex" - 3
Z$ = A$ ' SAVE off just in case
GoSub SPLIT_WHITE '     Split into words
IF M_VAR = 4  THEN
  X3$ = (SPLIT$(2)) ' Get Point X3
  Y3$ = (SPLIT$(3)) ' Get Point Y3
  Z3$ = (SPLIT$(4)) ' Get Point Z3
  END IF
      INPUT #1, A$ '     Get "endloop"
      INPUT #1, A$ '     Get "endfacet"
      X1 = VAL(X1$)
      Y1 = VAL(Y1$)
      Z1 = VAL(Z1$)
      X2 = VAL(X2$)
      Y2 = VAL(Y2$)
      Z2 = VAL(Z2$)
      X3 = VAL(X3$)
      Y3 = VAL(Y3$)
      Z3 = VAL(Z3$)
      IF  CR_OBJ = 1  OR  CR_OBJ = 3  THEN
      >LINE '   DRAW the TRIANGLE
       {
<PointXYZ [X1, Y1, Z1] ' Stasrting Point
<PointXYZ [X2, Y2, Z2] '   Via Point 2
<PointXYZ [X3, Y3, Z3] '   Via Point 3
<PointXYZ [X1, Y1, Z1] '   Return to Point 1
       }
       END IF
      IF CR_OBJ <> 1  THEN ' CREATE an XYZ data file ?
print #3, X1, Y1, Z1
print #3, X2, Y2, Z2
print #3, X3, Y3, Z3
print #3, X1, Y1, Z1
print #3, ""
END IF
      IF ANALYZE = 1  THEN ' CREATE ANALYSIS data
print #4, X1, Y1, Z1
print #4, X2, Y2, Z2
print #4, X3, Y3, Z3
END IF
    END IF
    LOOP
RETURN

STL_BINARY: '  STL - BINARY - data file
  SYS(36) = 2 '    EXTRA - Silent
  CLOSE 1 '    CLOSE the (I)nput File
  OPEN "R", 1, FL_NAME$, 1 '    OPEN  the (R)andom (BIN) Fl
FL_BYTE = 0

  REC_LEN = 80 '    HEADER Length
    GoSub GET_REC ' Read it
      HDR$ = ""
      FOR i = 1 to LEN(M_A$)
IF MID$(M_A$,i,1) >= " "  THEN
  HDR$ = HDR$ + MID$(M_A$,i,1)
  END IF
NEXT I
    print #2, "  Header: ", HDR$ ' LOG Header data
  REC_LEN = 4 '    TRIANGLE Count
    GoSub GET_REC ' Read it
      A$ = M_A$
      GoSub UINT_TO_NUM ' Unsigned Integer Conv
      TRI_CNT = M_NUM ' Get TRIANGLE Count
      print #2, "  Triangle Count: ", TRI_CNT

  REC_LEN = 50 '    TRIANGLE DEFINITION Length
  FOR P_TRI = 1 to TRI_CNT
    IF (P_TRI MOD 150) = 0  THEN ' PERIODIC LOGGING
      print #2, "    Mid Count at: ", SYS$(8), "   ", SYS$(7), "  ", P_TRI
      END IF
    IF (P_TRI MOD 1000) = 0   THEN ' Time to REFRESH the Scrn
      REGEN
      END IF
    GoSub GET_REC ' Read next Triangle
      TRI$ = M_A$ '   Be Sure to Keep it
' SKIPPING Outside Pntr
      A$ = MID$(TRI$,13,4) '   TRI - X1
GoSub REAL_TO_NUM ' Conv to Number
X1 = M_NUM
      A$ = MID$(TRI$,17,4) '   TRI - Y1
GoSub REAL_TO_NUM ' Conv to Number
Y1 = M_NUM
      A$ = MID$(TRI$,21,4) '   TRI - Z1
GoSub REAL_TO_NUM ' Conv to Number
Z1 = M_NUM
      A$ = MID$(TRI$,25,4) '   TRI - X2
GoSub REAL_TO_NUM ' Conv to Number
X2 = M_NUM
      A$ = MID$(TRI$,29,4) '   TRI - Y2
GoSub REAL_TO_NUM ' Conv to Number
Y2 = M_NUM
      A$ = MID$(TRI$,33,4) '   TRI - Z2
GoSub REAL_TO_NUM ' Conv to Number
Z2 = M_NUM
      A$ = MID$(TRI$,37,4) '   TRI - X3
GoSub REAL_TO_NUM ' Conv to Number
X3 = M_NUM
      A$ = MID$(TRI$,41,4) '   TRI - Y3
GoSub REAL_TO_NUM ' Conv to Number
Y3 = M_NUM
      A$ = MID$(TRI$,45,4) '   TRI - Z3
GoSub REAL_TO_NUM ' Conv to Number
Z3 = M_NUM

    IF  CR_OBJ = 1  OR  CR_OBJ = 3  THEN '    Draw the Object ?
    >LINE '    DRAW the TRIANGLE
      {
       <PointXYZ [X1, Y1, Z1] ' Starting Point
       <PointXYZ [X2, Y2, Z2] '   Via Point 2
       <PointXYZ [X3, Y3, Z3] '   Via Point 3
       <PointXYZ [X1, Y1, Z1] '   Return to Point 1
      }
    END IF
    IF CR_OBJ <> 1  THEN ' CREATE an XYZ data file?
print #3, X1, Y1, Z1
print #3, X2, Y2, Z2
print #3, X3, Y3, Z3
print #3, X1, Y1, Z1
print #3, ""
END IF
    IF ANALYZE = 1  THEN ' CREATE ANALYSIS data
print #4, X1, Y1, Z1
print #4, X2, Y2, Z2
print #4, X3, Y3, Z3
END IF
   
    NEXT P_TRI
RETURN

GET_REC: '  Entering - GET_REC
  IF REC_LEN < 1  THEN ' Is a Record Len defined
print #2, "BAD RECORD LENGTH PASSED - to GET_REC: ", REC_LEN
GoTo THE_END '   B_A_I_L
END IF
  M_A$ = "" '    CLEAR return area
  for M_I = 1 to REC_LEN '    For each byte ...
    GET 1, (FL_BYTE + M_I), A$ ' GET the next one
    IF EOF(1)  THEN
M_I = REC_LEN + 1
M_A$ = ""
END IF
    M_A$ = M_A$ + A$ ' Accumulate it in RET
    NEXT M_I
  IF LEN(M_A$) <> REC_LEN  THEN '    Is RETURN area proper size
print #2, "BAD RECORD READ - in GET_REC: ", REC_LEN, LEN(M_A$)
GoTo THE_END ' B_A_I_L
END IF
  FL_BYTE = FL_BYTE + REC_LEN '    LAST File Byte Read
RETURN

SPLIT_WHITE:
  FOR M_IDX = 1 to 100 '  CLEAR DESTINATIONs
    SPLIT$(M_IDX) = "" '   One at a time
    NEXT I
  M_IDX = 1 '  Where to start within  -  A$
  M_L   = LEN(A$) '  Where must I stop
  M_VAR = 1 '  Index for Array Offset

  DO WHILE  M_IDX <= M_L '  START THE PARSING
    M_CH$ = MID$(A$,M_IDX,1) '    My (next) Character
    IF  SPLIT$(M_VAR) <> ""  AND  _ '  TIME for the NEXT entry ?
   M_CH$ <= " "  THEN
      M_VAR = M_VAR + 1 '    INCREMENT Index Pointer
      IF M_VAR = 100  THEN '    LAST VARIABLE available
   SPLIT$(M_VAR) = MID$(A$,M_IDX,9999) '   SAVE THE REST here
   M_CH = " " '   Clear CHAR Buffer
   M_IDX = M_L '   Set END OF INPUT
   END IF
      END IF
    IF  M_CH$ > " "  THEN '  A character needs saving
      SPLIT$(M_VAR) = SPLIT$(M_VAR) + M_CH$ '    SAVE the character at IDX
      END IF
    M_IDX = M_IDX + 1 '    Move on down the string
    LOOP
RETURN

TO_LOWER: '  INSURE STRING IS
  M_IDX = 1 '    lower - case
  M_L   = LEN(A$) ' String length
  M_A$ = "" '    Converted $
  DO WHILE  M_IDX  <=  M_L '    FOR EACH CHARACTER
    M_X = ASC(MID$(A$,M_IDX,1)) ' Get it's ASCII
    IF M_X > 64  AND M_X < 96  THEN ' IF - UPPER
M_X = M_X + 32 '   MAKE lower
END IF
    M_A$ = M_A$ + CHR$(M_X) '    BUILD output $
    M_IDX = M_IDX + 1
    LOOP
RETURN


' ==================================================================

UINT_TO_NUM: '  UNSIGNED INTEGER -> Number
'  RETURNS  -  M_NUM
  IF  ENDIAN < 0  THEN '  LITTLE ENDIAN on input ! ! !
    M_B$ = "" ' CLEAR Destination
    for M_I = LEN(A$) to 1  step -1 ' START Converting it
M_B$ = M_B$ + MID$(A$,M_I,1) '   One Byte at a time
NEXT M_I '   Until done
    A$ = M_B$ '  Set back into input
    END IF
  M_X = 0 '  CLEAR Accumulator
  M_BYTES = LEN(A$) '  How many bytes passed in  ?
  M_BITS = M_BYTES * 8 '    Get Number of bits
  FOR  M_I = 1 to LEN(A$) '  For EACH Input Byte
    M_X = M_X * 2^8 '    Bump Value for next Byte
    IF  MID$(A$,M_I,1) > CHR$(0)  THEN '    Non - Zero ?
M_X = M_X + ASC(MID$(A$,M_I,1)) '    Add next BYTE value
END IF
    NEXT M_I
  M_NUM = M_X '    SET Return Value
' Message "UINT -> Number:  ", LEN(A$), ",  ", M_NUM
RETURN '    RETURN from - UINT_TO_NUM

REAL_TO_NUM: '  CHAR_FLOAT -> Number
' IF  ENDIAN < 0  THEN '  LITTLE ENDIAN on input ! ! !
' ' HANDLED IN - UINT_TO_NUM

  IF LEN(A$) =  8  THEN '  SYSTEM STANDARD - DOUBLE  ?
    M_NUM = CVS(A$) '    YES - SYSTEM - CVS(A$)
    RETURN ' RETURN to caller
    END IF
  GoSub UINT_TO_NUM '    Go get the whole thing
  IF LEN(A$) =  4  THEN '  SINGLE - FLOAT -> Number
    M_SGN_CNT = 1 '    SIGN Bits
    M_EXP_CNT = 8 '    EXPONENT Bits
    M_SIG_CNT = 23 '    SIGNIFICAND Bits
    M_EXP_BIAS = 127 '    Exponent Bias
    END IF
  IF LEN(A$) =  2  THEN '  HALF - FLOAT -> Number
    M_SGN_CNT = 1 '    SIGN Bits
    M_EXP_CNT = 5 '    EXPONENT Bits
    M_SIG_CNT = 10 '    SIGNIFICAND Bits
    M_EXP_BIAS = 15 '    Exponent Bias
    END IF
  M_X = M_SGN_CNT + M_EXP_CNT + M_SIG_CNT
  IF  M_X <> M_BITS  THEN
SYS(36) = 0
Message "M_BITS  Calculation Error: ", M_X, ",  ", M_BITS
END IF
  M_BITS = M_BITS - 1

  M_SGN = 1 '  CLEAR/SET Work Areas
  M_EXP = 0
  M_SIG = 0
  M_X = M_NUM
'  SIGN Bit Handling
  IF  M_X >= 2^M_BITS  THEN ' Bit On  ?
M_SGN = -1 '   SET Sign Bit
M_X = M_X - 2^M_BITS
END IF
  M_EXP =  INT(M_X / 2^(M_SIG_CNT)) '  EXPONENT Handling
    EXP_CK = M_EXP
    M_EXP = M_EXP - M_EXP_BIAS '    BIAS offset
  M_SIG =  M_X MOD 2^M_SIG_CNT '  SIGNIFICAND - split off bits
    SIG_CK = M_SIG
  M_X   =  M_SIG '    SAVE only it for working
  M_Y   =  M_SIG_CNT - 1 '    Number of BITs to work with
  M_SIG = 0 '    CLEAR result
    for  M_I = (M_SIG_CNT - 1) to 0  Step -1 '    SIGNIFICANT Bits Handling
      IF M_X >= 2^M_I  THEN '    Bit On  ?
M_SIG = M_SIG + 2^(0 - (M_Y - M_I) - 1) ' INC - Signif
M_X = M_X - 2^M_I ' Adjust  M_X
END IF
      NEXT M_I
    M_SIG = M_SIG + 1 '    ADD the implied "1.0"
  M_NUM = M_SGN * 2^M_EXP * M_SIG '  THE NUMBER to Return
'
'  NON-REAL NUMBER  -  Check
  IF  (EXP_CK = (2^M_EXP_CNT) - 1)  AND _ '  EXP - ALL BITS ON - NON-REAL
((SIG_CK = 0)  OR _ ' Infinity
(SIG_CK = ((2^M_SIG_CNT) - 1)))  THEN ' Not-a-Number
SYS(36) = 0
Message "SPECIAL CASE NUMBER located", _
CHR$(13), "INFINITY  or  ""NaN"""
M_NUM = 0 ' CLEAR RETURN Value
    END IF
RETURN


DUMP_REC: '  Entering - DUMP_IT
  PRECISION 0
  HEX$ = "0123456789ABCDEF" '    A "Hex Dump" of what was
  D_REC = D_REC + 1 '    Read in
  print #2, D_REC, " - |";
  for i = 1 to LEN(A$) '    FOR Each Byte ...
    IF MID$(A$,i,1) > CHR$(0)  THEN
      w = ASC(MID$(A$,i,1)) ' Get it's value
     ELSE
      w = 0
      END IF
    x = INT(w/16) + 1 ' Upper Nibble area
    y = w MOD 16 + 1 ' Lower Nibble area
    PRT$ = MID$(HEX$,x,1) + MID$(HEX$,y,1)
    print #2, PRT$ + " "; ' Print it
    IF W >= 20  AND w <=128  THEN ' If Printable
PRT_2$ = PRT_2$ + MID$(A$,i,1) '     Store it for later
      ELSE
PRT_2$ = PRT_2$ + "." '     or store a period
      END IF
    NEXT i
  IF LEN(PRT_2$) > 10  THEN print #2, "" ' Put new line out
  print #2, "|-----|", PRT_2$ ' Print printable values
PRT_2$ = "" '   CLEAR printable vals
RETURN