2

I have been trying to solve a bin packing problem using a Python script I found, but when I try to use it I get an error that I don't know how to solve.

The code that I'm trying to use is taken from here: Fitting known size polygons into irregular polygons using ArcGIS Desktop?

What I am doing is creating 2 shapefiles with polygonal entities and I create a geoprocessing toolbox, I add the python script and I open it. When I open the tool I enter the layers of the shapefiles as parameters, and I execute it.

Possibly this is a simple error, but the truth is that I don't know how to solve this problem.


import arcpy
from arcpy import env
from math import radians,sin,cos
env.overwriteOutput = True
infc=arcpy.GetParameterAsText(0)
outFC=arcpy.GetParameterAsText(1)
d=arcpy.Describe(infc); SR=d.spatialReference
W=1;L=2;A=0.99*W*L
fnet="in_memory/fnet"
erased="in_memory/fnet"

rotate polygon

def ShapeMake(pGon,angle): a=radians(angle) ARR=arcpy.Array() cX=cPoint.X;cY=cPoint.Y for part in pGon.boundary(): ar=arcpy.Array() for p in part: x,y=p.X-cX,p.Y-cY xN=cos(a)x+sin(a)y yN=-sin(a)x+cos(a)y pN=arcpy.Point(xN+cX,yN+cY) ar.add(pN) ARR.add(ar) pgonRotated=arcpy.Polygon(ARR,SR) return pgonRotated

create fishnet and count complete polygons

def fnetMake(): FNET=[] ext=rotated.extent oc='%s %s' %(ext.XMin,ext.YMin) ya='%s %s' %(ext.XMin,ext.YMax) cc='%s %s' %(ext.XMax,ext.YMax) arcpy.CreateFishnet_management(fnet, oc,ya, W, L,"","", "","NO_LABELS", rotated,"POLYGON") rects=arcpy.Clip_analysis(fnet, rotated, g) for chop in rects: if chop.area<A:continue FNET.append(chop) return FNET

g=arcpy.Geometry() PGON=arcpy.CopyFeatures_management(infc,g)[0] theList=[PGON];bigList=[]

nBefore=0 while True: for toCut in theList:

    ## FIND rotation to maximise complete rectangles

    nMax=0
    cPoint=toCut.centroid

    for i in range(36):
        angle=5*i
        rotated=ShapeMake(toCut,angle)
        squares=fnetMake()
        N=len(squares)
        if N&lt;=nMax:continue
        nMax=N
        keepers=squares[:]
        bestAngle=angle

    if nMax==0:continue
    arcpy.AddMessage(&quot;%s cell(s) found so far&quot; %nMax)

    for item in keepers:
        rotated=ShapeMake(item,-bestAngle)
        bigList.append(rotated)

if nBefore==len(bigList):break
nBefore=len(bigList)
arcpy.Erase_analysis(PGON, bigList, erased)
theList=arcpy.MultipartToSinglepart_management(erased, g)

arcpy.CopyFeatures_management(bigList,outFC)

 Traceback (most recent call last):
  File "C:\Users\pc\Desktop\script1.py", line 82, in <module>
    arcpy.CopyFeatures_management(bigList,outFC)
  File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\management.py", line 3373, in CopyFeatures
    raise e
  File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\management.py", line 3370, in CopyFeatures
    retval = convertArcObjectToPythonObject(gp.CopyFeatures_management(*gp_fixargs((in_features, out_feature_class, config_keyword, spatial_grid_1, spatial_grid_2, spatial_grid_3), True)))
  File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\geoprocessing\_base.py", line 511, in <lambda>
    return lambda *args: val(*gp_fixargs(args, True))
RuntimeError: Object: error durante ejecución de herramienta
 Falló al ejecutar (calculopoligono).

 Traceback (most recent call last):
File "C:\Users\pc\Desktop\script1.py", line 82, in 
arcpy.CopyFeatures_management(bigList,outFC)
File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\management.py", line 3373, in CopyFeatures
raise e
File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\management.py", line 3370, in CopyFeatures
retval = convertArcObjectToPythonObject(gp.CopyFeatures_management(*gp_fixargs((in_features, out_feature_class, config_keyword, spatial_grid_1, spatial_grid_2, spatial_grid_3), True)))
File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\geoprocessing_base.py", line 511, in 
return lambda *args: val(*gp_fixargs(args, True))
RuntimeError: Object: error durante ejecución de herramienta
Falló al ejecutar (calculopoligono).

KHibma
  • 16,786
  • 1
  • 31
  • 55
Robert22
  • 21
  • 4
  • 1
    Please remember to always include the code and error message as text in the body of the Question. – Vince Nov 27 '20 at 00:21
  • 2
    For this script to work you need one polygonal input containing single feature. 2nd is output that will store rectangular polygons with dimensions of W and L. So change 2nd type to output. – FelixIP Nov 27 '20 at 00:50

0 Answers0