SHARE
TWEET

Untitled

a guest Jun 27th, 2019 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Draws shape to modelspace
  2.  
  3. def drawShape(shape,msp):
  4.  
  5.     if(shape.type == 'Polygon'):
  6.        
  7.         drawLineRing(shape.exterior,msp)
  8.            
  9.         for linering in shape.interiors:
  10.        
  11.             #print(lineRing.type)
  12.        
  13.             drawLineRing(linering,msp)
  14.        
  15.             #smooth = smoothPoly(linering)
  16.        
  17.             #drawLineRing(smooth.exterior,msp)
  18.                
  19.     if(shape.type == 'MultiPolygon'):
  20.    
  21.         for geom in web.geoms:
  22.    
  23.             drawLineRing(geom.exterior,msp)
  24.            
  25.             for linering in geom.interiors:
  26.            
  27.                 #print(lineRing.type)
  28.                
  29.                 drawLineRing(linering,msp)
  30.                
  31.                 #smooth = smoothPoly(linering)
  32.            
  33.                 #drawLineRing(smooth.exterior,msp)
  34.        
  35. def drawLineRing(lineRing, msp):
  36.  
  37.  
  38.     #polyline= dxf.polyline()
  39.     #polyline.add_vertex([xArr[-1],yArr[-1]])
  40.     xArr,yArr = lineRing.xy
  41.        
  42.     msp.add_lwpolyline(zip(xArr,yArr))
  43.  
  44. def commonPoints(tri1, tri2):
  45.    
  46.     tri1Set = set(tri1)
  47.     tri2Set = set(tri2)
  48.    
  49.     if( len(tri1Set & tri2Set) > 1):
  50.         return True
  51.     else:    
  52.         return False    
  53.    
  54. def getDist(point1, point2):
  55.  
  56.     return math.sqrt(math.pow(point1[0]-point2[0],2)+math.pow(point1[1]-point2[1],2))
  57.    
  58. def getCircumcenter(triangle, points):
  59.  
  60.     triPoints = [points[triangle[0]],points[triangle[1]],points[triangle[2]]]
  61.    
  62.     #print(triPoints)
  63.    
  64.     d1 = getDist(triPoints[0],triPoints[1])
  65.     d2 = getDist(triPoints[1],triPoints[2])
  66.    
  67.     dist = max(d1,d2)
  68.    
  69.     c0 = Point(triPoints[0][0],triPoints[0][1]).buffer(dist).boundary
  70.     c1 = Point(triPoints[1][0],triPoints[1][1]).buffer(dist).boundary
  71.     c2 = Point(triPoints[2][0],triPoints[2][1]).buffer(dist).boundary
  72.    
  73.     line1 = shapely.geometry.LineString(c0.intersection(c1))
  74.     line2 = shapely.geometry.LineString(c1.intersection(c2))
  75.    
  76.     return line1.intersection(line2)
  77.    
  78. def towards4(x,y,angle,distance):
  79.  
  80.     x = x + math.sin(angle) * distance
  81.     y = y + math.cos(angle) * distance
  82.  
  83.     return [x,y]    
  84.    
  85. def towards3(start,angle,distance):
  86.  
  87.     x = start[0] + math.sin(angle) * distance
  88.     y = start[1] + math.cos(angle) * distance
  89.  
  90.     return [x,y]    
  91.    
  92. def findDistance(point1,point2):
  93.  
  94.     pointDx = point1[0] - point2[0]
  95.     pointDy = point1[1] - point2[1]
  96.  
  97.     return  math.hypot(pointDx,pointDy)
  98.    
  99. def towardsPoint(start,point,distance):
  100.  
  101.     totalDist = findDistance(start,point)
  102.    
  103.     frac = distance / totalDist
  104.    
  105.     x = start[0] * (1-frac) + point[0] * frac
  106.     y = start[1] * (1-frac) + point[1] * frac
  107.  
  108.     return [x,y]  
  109.    
  110. def smoothPoly(polygon, res = 100):
  111.  
  112.     reach = max(polygon.bounds) - min(polygon.bounds)
  113.  
  114.     center = polygon.centroid
  115.    
  116.     samplePoints = []
  117.     sampleDists  = np.zeros((res))
  118.    
  119.     blurPoints   = []
  120.    
  121.     #print(center.type)
  122.     #print(center)
  123.     #print(center.x)
  124.     #print(center.y)
  125.    
  126.     for i in range(res):
  127.    
  128.         angle = i/res * 2 * math.pi
  129.  
  130.         farPoint = towards4(center.x,center.y, angle, reach)
  131.        
  132.         line = shapely.geometry.LineString([center,farPoint])
  133.        
  134.         sample = line.intersection(polygon)
  135.        
  136.         samplePoints.append(sample)
  137.         sampleDists[i] = (getDist([center.x,center.y],[sample.x,sample.y]))
  138.        
  139.     blurDists = gaussian_filter1d(sampleDists,5,mode='wrap')
  140.    
  141.     for i in range(res):
  142.    
  143.         angle = i/res * 2 * math.pi
  144.        
  145.         blurPoint = towards4(center.x,center.y,angle,blurDists[i])
  146.        
  147.         blurPoints.append(blurPoint)
  148.        
  149.     return shapely.geometry.Polygon(blurPoints)
  150.  
  151. if __name__ == "__main__":
  152.     print('main')
  153.    
  154.     import ezdxf
  155.     import shapely
  156.     import math
  157.    
  158.     from shapely import geometry
  159.    
  160.     dwg = ezdxf.new('AC1015')
  161.     modelspace = dwg.modelspace()
  162.    
  163.     preview = ezdxf.new('AC1015')
  164.     pModelspace = preview.modelspace()
  165.    
  166.     positiveSpace = geometry.Polygon([[0,0],[0,30],[30,30],[30,0]])
  167.     negativeSpace = geometry.Polygon([[2,2],[2,28],[28,28],[28,2]])
  168.    
  169.     #drawShape(positiveSpace,modelspace)
  170.     drawShape(positiveSpace,pModelspace)
  171.    
  172.     #""" # uncomment for other demo
  173.     for i in range(3):
  174.        
  175.  
  176.         cut = geometry.Point(25,25).buffer(8 * (1+i))
  177.        
  178.         cut = cut.intersection(negativeSpace)
  179.        
  180.         drawShape(cut,pModelspace) # stack shapes on preview
  181.        
  182.         # seperate layers in real cut
  183.         layerNegative = shapely.affinity.translate(cut,xoff=30*i)
  184.         drawShape(layerNegative,modelspace)
  185.        
  186.         layerPositive = shapely.affinity.translate(positiveSpace,xoff=30*i)
  187.         drawShape(layerPositive,modelspace)
  188.      # """  # uncomment for other demo  
  189.    
  190.     """ # comment for fancier demo
  191.     for i in range(5):
  192.        
  193.    
  194.         circs = []
  195.        
  196.         for j in range(290):
  197.        
  198.             xPos = 30/2
  199.             yPos = 30/2
  200.            
  201.             angle = (1 + 5 ** 0.5) / 2 * math.pi * j  # golden ratio to get angles
  202.            
  203.             point = towards4(xPos,yPos,angle, math.sqrt(j))
  204.        
  205.             circs.append(geometry.Point(point[0],point[1]).buffer(.25 * (1+i)))
  206.        
  207.        
  208.         #circ1 = geometry.Point(25,25).buffer(3 * (1+i))
  209.         #circ2 = geometry.Point(20,9).buffer(3 * (1+i))
  210.         #circ3 = geometry.Point(7,12).buffer(3 * (1+i))
  211.        
  212.         #cut =  ops.cascaded_union([circ1,circ2,circ3])
  213.         cut =  ops.cascaded_union(circs)
  214.        
  215.         cut = cut.intersection(negativeSpace)
  216.        
  217.         drawShape(cut,pModelspace) # stack shapes on preview
  218.        
  219.         # seperate layers in real cut
  220.         layerNegative = shapely.affinity.translate(cut,xoff=30*i)
  221.         drawShape(layerNegative,modelspace)
  222.        
  223.         layerPositive = shapely.affinity.translate(positiveSpace,xoff=30*i)
  224.         drawShape(layerPositive,modelspace)
  225.     """ # comment for fancier demo
  226.    
  227.     dwg.saveas('layers.dxf')
  228.     preview.saveas('preview.dxf')
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top