STLファイルビューワ

 投稿者:しばっち  投稿日:2013年 9月 2日(月)20時07分30秒
  !'簡易STLファイルビューワ(バイナリー形式のみ)

OPTION CHARACTER BYTE
OPTION ARITHMETIC NATIVE
OPTION ANGLE DEGREES
LET ZTH=0          ! z軸のまわりの回転角
LET XTH=0          ! x軸のまわりの回転角初期値
LET YTH=0          ! y軸のまわりの回転角初期値
DIM M(4,4),POINT(4),ROTX(4,4),ROTY(4,4)
MAT M=ROTATE(ZTH)
SET POINT STYLE 1
FILE GETNAME F$, "STLファイル|*.stl"
IF F$="" THEN STOP
OPEN #1:NAME F$
FOR I=1 TO 5
   CHARACTER INPUT #1:A$(I:I)
NEXT I
IF LCASE$(A$)="solid" THEN
   PRINT "STLファイル アスキー形式です"
   CLOSE #1
   STOP
END IF
FOR I=1 TO 75
   CHARACTER INPUT #1:A$
NEXT I
LET A$=""
FOR I=1 TO 4
   CHARACTER INPUT #1:A$(I:I)
NEXT I
LET N=CVL(A$) !'三角形ポリゴン数
!'IF N>100000 THEN
!'   PRINT "ファイルが大きすぎです"
!'   CLOSE #1
!'   STOP
!'END IF
DIM XX(N,3),YY(N,3),ZZ(N,3)
FOR K=1 TO N
   FOR J=1 TO 12 !'法線データ(x,y,z) 3×4byte(空読み)
      CHARACTER INPUT #1:A$
   NEXT J
   FOR I=1 TO 3
      LET A$=""
      FOR J=1 TO 4 !'x座標
         CHARACTER INPUT #1:A$(J:J)
      NEXT J
      LET XX(K,I)=CVS(A$)
      LET A$=""
      FOR J=1 TO 4 !'y座標
         CHARACTER INPUT #1:A$(J:J)
      NEXT J
      LET YY(K,I)=CVS(A$)
      LET A$=""
      FOR J=1 TO 4 !'z座標
         CHARACTER INPUT #1:A$(J:J)
      NEXT J
      LET ZZ(K,I)=CVS(A$)
      LET XMIN=MIN(XMIN,XX(K,I))
      LET XMAX=MAX(XMAX,XX(K,I))
      LET YMIN=MIN(YMIN,YY(K,I))
      LET YMAX=MAX(YMAX,YY(K,I))
      LET ZMIN=MIN(ZMIN,ZZ(K,I))
      LET ZMAX=MAX(ZMAX,ZZ(K,I))
      LET MX=MX+XX(K,I)
      LET MY=MY+YY(K,I)
      LET MZ=MZ+ZZ(K,I)
   NEXT I
   LET A$=""
   CHARACTER INPUT #1:A$
   CHARACTER INPUT #1:A$
NEXT K
CLOSE #1
LET MX=MX/K/3
LET MY=MY/K/3
LET MZ=MZ/K/3
LET RANGE=MAX(XMAX-XMIN,MAX(YMAX-YMIN,ZMAX-ZMIN))
LET XDT=RND-.5
LET YDT=RND-.5
LET LMIN=1E+10
LET LMAX=-1E+10
LOCATE VALUE NOWAIT(1),RANGE  0 TO 2,AT 1 : SCALE
LOCATE VALUE NOWAIT(2),RANGE -3 TO 5,AT 1 : SPEED
LOCATE VALUE NOWAIT(3),RANGE -RANGE TO RANGE,AT 0 : XMOVE
LOCATE VALUE NOWAIT(4),RANGE -RANGE TO RANGE,AT 0 : YMOVE
LOCATE VALUE NOWAIT(5),RANGE -RANGE TO RANGE,AT 0 : ZMOVE
DO
   LOCATE VALUE NOWAIT(1): SCALE
   LOCATE VALUE NOWAIT(2): SPEED
   LOCATE VALUE NOWAIT(3): XMOVE
   LOCATE VALUE NOWAIT(4): YMOVE
   LOCATE VALUE NOWAIT(5): ZMOVE
   MAT ROTX=IDN ! x軸のまわりの回転
   LET ROTX(2,2)=COS(XTH)
   LET ROTX(2,3)=SIN(XTH)
   LET ROTX(3,2)=-SIN(XTH)
   LET ROTX(3,3)=COS(XTH)
   MAT ROTY=IDN ! y軸のまわりの回転
   LET ROTY(1,1)=COS(YTH)
   LET ROTY(1,3)=-SIN(YTH)
   LET ROTY(3,1)=SIN(YTH)
   LET ROTY(3,3)=COS(YTH)
   MAT M=M *  ROTX * ROTY
   SET DRAW MODE HIDDEN
   CLEAR
   FOR I=1 TO N
      CALL PLOT(XX(I,1),YY(I,1),ZZ(I,1))
      CALL PLOT(XX(I,2),YY(I,2),ZZ(I,2))
      CALL PLOT(XX(I,3),YY(I,3),ZZ(I,3))
      CALL PLOT(XX(I,1),YY(I,1),ZZ(I,1))
      PLOT LINES
   NEXT I
   IF FL=0 THEN
      SET WINDOW LMIN*1.5,LMAX*1.5,LMIN*1.5,LMAX*1.5
      LET WW=(LMAX-LMIN)*1.5
   END IF
   LET FL=1
   SET DRAW MODE EXPLICIT
   MOUSE POLL X,Y,L,R
   IF R<>0 THEN STOP
   LET XTH=0
   LET YTH=0
   IF L<>0 THEN
      DO WHILE L<>0
         MOUSE POLL X,Y,L,R
      LOOP
      LET XDT=-(Y-Y0)/WW*5
      LET YDT= (X-X0)/WW*5
      LET XDT=MAX(-5,MIN(5,XDT))
      LET YDT=MAX(-5,MIN(5,YDT))
   ELSE
      LET XTH=XTH+XDT*SPEED
      LET YTH=YTH+YDT*SPEED
   END IF
   LET X0=X
   LET Y0=Y
LOOP

SUB PLOT(X,Y,Z)
   LET POINT(1)=X-MX+XMOVE
   LET POINT(2)=Y-MY+YMOVE
   LET POINT(3)=Z-MZ+ZMOVE
   MAT POINT=POINT*M
   IF FL=0 THEN
      LET LMIN=MIN(LMIN,POINT(1))
      LET LMAX=MAX(LMAX,POINT(1))
      LET LMIN=MIN(LMIN,POINT(2))
      LET LMAX=MAX(LMAX,POINT(2))
   ELSE
      PLOT LINES:POINT(1)*SCALE,POINT(2)*SCALE;
   END IF
END SUB
END

EXTERNAL  FUNCTION CVS(A$)
OPTION ARITHMETIC NATIVE
OPTION CHARACTER BYTE
OPTION BASE 0
DIM B(32)
LET  A$=LEFT$(A$,4)
LET  K = 0
FOR I = 4 TO 1 STEP -1
   LET  D$ = MID$(A$, I, 1)
   FOR J = 0 TO 7
      IF BITAND(ORD(D$),2 ^ (7 - J))<>0 THEN LET  B(K) = 1 ELSE LET  B(K) = 0
      LET  K = K + 1
   NEXT J
NEXT I
FOR I = 1 TO 8
   LET  E = E + B(I) * 2 ^ (8 - I)
NEXT I
LET  E=E-127
FOR I = 9 TO 31
   LET  S = S + B(I) * 2 ^ (8 - I)
NEXT I
LET  X=2^E*(S+1)
IF B(0)=1 THEN LET  X=-X
LET  CVS=X
END FUNCTION

EXTERNAL  FUNCTION CVL(A$)
OPTION ARITHMETIC NATIVE
OPTION CHARACTER BYTE
DECLARE NUMERIC A
LET  A$=LEFT$(A$,4)
LET  A=ORD(A$(1:1))+ORD(A$(2:2))*256+ORD(A$(3:3))*256^2+ORD(A$(4:4))*256^3
IF A>=2^31-1 THEN LET  A=A-2^32
LET  CVL=A
END FUNCTION
 

戻る