Advertisement
Guest User

Untitled

a guest
Apr 27th, 2012
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. filename = "/Users/andy2/Documents/work/Rear Right.stl"
  2. ps = 1 % conversion from mm RD-GL coords
  3. thickness = 0.302 % thickness of layers
  4. pagewidth = 285; % paper size
  5. pageheight = 195;
  6.  
  7. [F V C] = stl_read(filename);
  8.  
  9. X = [V(F(:,1),1) V(F(:,2),1) V(F(:,3),1)]; % X coords for each face
  10. Y = [V(F(:,1),2) V(F(:,2),2) V(F(:,3),2)]; %Y
  11. Z = [V(F(:,1),3) V(F(:,2),3) V(F(:,3),3)]; %Z
  12.  
  13. T = Z; %quick and dirty rotation
  14. Z = X;
  15. X = T;
  16.  
  17. clear T
  18.  
  19. % find extents
  20.  
  21. zmin= min(Z(:))
  22. zmax = max(Z(:))
  23.  
  24. page_x = 0; page_y = 0;
  25.  
  26. %for z = zmin:.005:zmax
  27.  
  28. z = mean([zmax zmin])% test porpoises
  29.  
  30. I1 = find(sum((Z>z),2) == 1); % Index of faces with 1 vertex greater than z
  31. I2 = find(sum((Z>z),2) == 2); % index of faces with 2 > z
  32.  
  33. C = [];
  34.  
  35. for a = 1:length(I1)
  36. [zs i] = sort(Z(I1(a),:)) ;% put biggest Z last, and store order
  37. xs = X(I1(a),i);
  38. ys = Y(I1(a),i);
  39.  
  40. x1 = xs(1)+(xs(3)-xs(1))*(z-zs(1))/(zs(3)-zs(1));
  41. x2 = xs(2)+(xs(3)-xs(2))*(z-zs(2))/(zs(3)-zs(2));
  42.  
  43. y1 = ys(1)+(ys(3)-ys(1))*(z-zs(1))/(zs(3)-zs(1));
  44. y2 = ys(2)+(ys(3)-ys(2))*(z-zs(2))/(zs(3)-zs(2));
  45.  
  46. C = [C; x1 y1 x2 y2];
  47.  
  48. end
  49.  
  50. for a = 1:length(I2)
  51.  
  52. [zs i] = sort(Z(I2(a),:)) ;% put biggest Z last, and store order
  53. xs = X(I2(a),i);
  54. ys = Y(I2(a),i);
  55.  
  56. x1 = xs(2)+(xs(1)-xs(2))*(z-zs(2))/(zs(1)-zs(2));
  57. x2 = xs(3)+(xs(1)-xs(3))*(z-zs(3))/(zs(1)-zs(3));
  58.  
  59. y1 = ys(2)+(ys(1)-ys(2))*(z-zs(2))/(zs(1)-zs(2));
  60. y2 = ys(3)+(ys(1)-ys(3))*(z-zs(3))/(zs(1)-zs(3));
  61.  
  62. C = [C; x1 y1 x2 y2];
  63.  
  64.  
  65. end
  66.  
  67. xmax = max(max(C(:,[1 3])));
  68. xmin = min(min(C(:,[1 3])));
  69. ymax = max(max(C(:,[2 4])));
  70. ymin = min(min(C(:,[2 4])));
  71.  
  72. clg
  73. hold on
  74. plot (xmin, ymin, 'b');
  75. legend("off")
  76.  
  77. %plot([0;0;xmax-xmin;xmax-xmin;0] , [ymax-ymin;0;0;ymax-ymin;ymax-ymin])
  78. %plot([C(:,1) C(:,3)]' , [C(:,2) C(:,4)]')
  79. %hold off
  80.  
  81. if (xmax-xmin > pagewidth | ymax-ymin > pageheight)
  82. disp("Page not large enough for object")
  83. stop
  84. endif
  85.  
  86. C = C * ps; % Convert to points/mm/whatever
  87.  
  88. if (page_y + ymax - ymin > pageheight) % top of page reached
  89.  
  90. if (f ~= 0) % file already open
  91.  
  92. fprintf(f, '%%%%EOF\n')
  93. fclose(f)
  94. fputs(p, "SP 0;\n")
  95.  
  96. % do something with plotter here
  97. endif
  98. endif
  99.  
  100.  
  101. f = fopen(['/Users/andy2/Documents/gash' num2str(z) '.eps'], 'wt');
  102.  
  103. fprintf(f, '%%!PS-Adobe-3.0 EPSF-3.0\n')
  104. fprintf(f, '%%%%BoundingBox: %0.2f %0.2f %0.2f %0.2f\n', 0, 0, (xmax-xmin)*ps*1.02, (ymax-ymin)*ps*1.02)
  105. fprintf(f, '0.75 setlinewidth\n')
  106.  
  107. %Try to string line segments together into loops.
  108.  
  109. g = 1:length(C); % use this vector to store indices to the currently unassigned segments
  110.  
  111. lastx = 0 ; lasty = 0;
  112.  
  113. while (any(g))
  114. % find nearest (x,y) to (lastx, lasty)
  115.  
  116. gi = 1; % starting position
  117.  
  118. fprintf(f, '%0.2f %0.2f moveto\n', C(g(gi), 1)-xmin*ps, C(g(gi), 2)-ymin*ps);
  119. fprintf(f, '%0.2f %0.2f lineto\n', C(g(gi), 3)-xmin*ps, C(g(gi), 4)-ymin*ps);
  120. plot([C(g(gi), 1) C(g(gi), 3)] , [C(g(gi), 2) C(g(gi), 4)], 'b');
  121.  
  122. lastx = C(g(gi),3) ; lasty = C(g(gi), 4);
  123.  
  124. do
  125.  
  126. g = [g(1:gi-1) g(gi+1:end)]; % remove used index from list
  127.  
  128. % find x and y match looking in both start and end
  129. gi = [find((C(g,1) == lastx) & (C(g,2) == lasty)) ; ...
  130. find((C(g,3) == lastx) & (C(g,4) == lasty))];
  131.  
  132. if any(gi)
  133. gi = gi(1); % choose the first match
  134. xi = [3 1](find(C(g(gi),[1 3]) == lastx)); % find the new positions
  135. yi = [4 2](find(C(g(gi),[2 4]) == lasty));
  136. if (length(xi) == 1 | length(yi) == 1) % needed for zero-length segments
  137. fprintf(f, '%0.2f %0.2f lineto\n', ...
  138. C(g(gi), xi(1))-xmin*ps, C(g(gi), yi(1))-ymin*ps);
  139.  
  140. plot([lastx C(g(gi), xi(1))], [lasty C(g(gi), yi(1))], 'b')
  141.  
  142. lastx = C(g(gi),xi(1)) ; lasty = C(g(gi), yi(1));
  143. endif
  144. endif
  145.  
  146. if !any(gi)
  147. fprintf(f, 'stroke\n')
  148. endif
  149.  
  150. until !any(gi) % ie no matches for last end point
  151.  
  152.  
  153. endwhile
  154.  
  155.  
  156.  
  157. fprintf(f, '%%%%EOF\n')
  158.  
  159. fclose(f)
  160.  
  161. %end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement