Python script exported from ModelBuilder won't run

3580
8
03-06-2014 02:56 PM
tamerali
New Contributor III
hi to all

i exported mode builder to python but it is not work , plz any ideas , it work will in arcgis

thank you

# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------
# test.py
# Created on: 2014-03-07 03:11:40.00000
#   (generated by ArcGIS/ModelBuilder)
# Description:
# ---------------------------------------------------------------------------

# Set the necessary product code
# import arcinfo


# Import arcpy module
import arcpy
arcpy.env.overwriteOutput = True
# Load required toolboxes
arcpy.ImportToolbox("Model Functions")


# Local variables:
cad_distace_DWG = "E:\\country\\cad_distace.DWG"
acad1_gdb = "E:\\country\\acad1.gdb"
Polyline = "E:\\country\\acad1.gdb\\cad\\Polyline"
point = "E:\\country\\acad1.gdb\\point"
cad = "E:\\country\\acad1.gdb\\cad"
south1 = "E:\\country\\acad1.gdb\\south1"
west = "E:\\country\\acad1.gdb\\west"
east = "E:\\country\\acad1.gdb\\east"
north = "E:\\country\\acad1.gdb\\north"
south = "E:\\country\\acad1.gdb\\south"
north1 = "E:\\country\\acad1.gdb\\north1"
east1 = "E:\\country\\acad1.gdb\\east1"
west1 = "E:\\country\\acad1.gdb\\west1"
west_dis = "E:\\country\\acad1.gdb\\west_dis"
east_dis = "E:\\country\\acad1.gdb\\east_dis"
south_dis = "E:\\country\\acad1.gdb\\south_dis"
north_dis = "E:\\country\\acad1.gdb\\north_dis"
final_distance = "E:\\country\\acad1.gdb\\final_distance"

# Process: CAD to Geodatabase
arcpy.CADToGeodatabase_conversion("E:\\country\\cad_distace.DWG", acad1_gdb, "cad", "25000000", "PROJCS['WGS_1984_UTM_Zone_38N',GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Transverse_Mercator'],PARAMETER['False_Easting',500000.0],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',45.0],PARAMETER['Scale_Factor',0.9996],PARAMETER['Latitude_Of_Origin',0.0],UNIT['Meter',1.0]];-5120900 -9998100 10000;-100000 10000;-100000 10000;0.001;0.001;0.001;IsHighPrecision")

# Process: Iterate Feature Classes
arcpy.IterateFeatureClasses_mb(cad, "", "LINE", "NOT_RECURSIVE")

# Process: Feature Vertices To Points
arcpy.FeatureVerticesToPoints_management(Polyline, point, "ALL")

# Process: Select (8)
arcpy.Select_analysis(point, west1, "Layer ='?'")

# Process: Select (2)
arcpy.Select_analysis(Polyline, west, "Layer ='???'")

# Process: Spatial Join
arcpy.SpatialJoin_analysis(west1, west, west_dis, "JOIN_ONE_TO_ONE", "KEEP_ALL", "Layer \"Layer\" true true false 255 Text 0 0 ,First,#,E:\\country\\acad1.gdb\\west1,Layer,-1,-1", "CLOSEST", "", "distance")

# Process: Select (7)
arcpy.Select_analysis(point, east1, "Layer ='?'")

# Process: Select (3)
arcpy.Select_analysis(Polyline, east, "Layer ='????'")

# Process: Spatial Join (2)
arcpy.SpatialJoin_analysis(east1, east, east_dis, "JOIN_ONE_TO_ONE", "KEEP_ALL", "Layer \"Layer\" true true false 255 Text 0 0 ,First,#,E:\\country\\acad1.gdb\\east1,Layer,-1,-1", "CLOSEST", "", "distance")

# Process: Select
arcpy.Select_analysis(point, south1, "Layer ='?'")

# Process: Select (5)
arcpy.Select_analysis(Polyline, south, "Layer ='??????'")

# Process: Spatial Join (3)
arcpy.SpatialJoin_analysis(south1, south, south_dis, "JOIN_ONE_TO_ONE", "KEEP_ALL", "Layer \"Layer\" true true false 255 Text 0 0 ,First,#,E:\\country\\acad1.gdb\\south1,Layer,-1,-1", "CLOSEST", "", "distance")

# Process: Select (6)
arcpy.Select_analysis(point, north1, "Layer ='????'")

# Process: Select (4)
arcpy.Select_analysis(Polyline, north, "Layer ='??????'")

# Process: Spatial Join (4)
arcpy.SpatialJoin_analysis(north1, north, north_dis, "JOIN_ONE_TO_ONE", "KEEP_ALL", "Layer \"Layer\" true true false 255 Text 0 0 ,First,#,E:\\country\\acad1.gdb\\north1,Layer,-1,-1", "CLOSEST", "", "distance")

# Process: Merge
arcpy.Merge_management("E:\\country\\acad1.gdb\\west_dis;E:\\country\\acad1.gdb\\east_dis;E:\\country\\acad1.gdb\\south_dis;E:\\country\\acad1.gdb\\north_dis", final_distance, "Join_Count \"Join_Count\" true true false 0 Long 0 0 ,First,#,E:\\country\\acad1.gdb\\west_dis,Join_Count,-1,-1,E:\\country\\acad1.gdb\\east_dis,Join_Count,-1,-1,E:\\country\\acad1.gdb\\south_dis,Join_Count,-1,-1,E:\\country\\acad1.gdb\\north_dis,Join_Count,-1,-1;distance \"distance\" true true false 0 Double 0 0 ,First,#,E:\\country\\acad1.gdb\\west_dis,distance,-1,-1,E:\\country\\acad1.gdb\\east_dis,distance,-1,-1,E:\\country\\acad1.gdb\\south_dis,distance,-1,-1,E:\\country\\acad1.gdb\\north_dis,distance,-1,-1;TARGET_FID \"TARGET_FID\" true true false 0 Long 0 0 ,First,#,E:\\country\\acad1.gdb\\west_dis,TARGET_FID,-1,-1,E:\\country\\acad1.gdb\\east_dis,TARGET_FID,-1,-1,E:\\country\\acad1.gdb\\south_dis,TARGET_FID,-1,-1,E:\\country\\acad1.gdb\\north_dis,TARGET_FID,-1,-1;Layer \"Layer\" true true false 255 Text 0 0 ,First,#,E:\\country\\acad1.gdb\\west_dis,Layer,-1,-1,E:\\country\\acad1.gdb\\east_dis,Layer,-1,-1,E:\\country\\acad1.gdb\\south_dis,Layer,-1,-1,E:\\country\\acad1.gdb\\north_dis,Layer,-1,-1")
Tags (2)
0 Kudos
8 Replies
RichardFairhurst
MVP Honored Contributor
Iterators are not tranferable to python.  You have to use a python loop syntax like for, do while, etc.  You also may have to use a cursor to read the data to interate.  Plenty of examples on the forum and other python sites about loops and tons of examples of cursors on the Python forum.
0 Kudos
tamerali
New Contributor III
thank you Richard , can i run the model builder through python script ?
0 Kudos
RichardFairhurst
MVP Honored Contributor
thank you Richard , can i run the model builder through python script ?


Model Builder scripts exported to Python only work for tools that are not specific to Model Builder.  These tools are in the tool box tools.  Iterators are not added to the model from a toolbox tool and have to be added to a model from a Model Builder specific menu.  Nothing added to a model that way can be exported to Python.  You would have to remove the Iterator and only export the portions of the model that came from the toolboxes inside the iteration.  Then in Python you would have to write Python code that does the same function as an Iterator in ModelBuilder.

So the answer is no the moment you have to use a ModelBuilder specific process to make your model work.  There is no point spending a lot of time building Iterators in ModelBuilder if your real goal is to run the model process in a Python script.  You should only spend time using an Iterator in ModelBuilder to verify that the Iterator makes your model do what you need to do in ModelBuilder, but when you want to go to the Python script the Iterator part of the model has to be removed and only the real geoprocessing tools from the toolboxes can be exported to the python script.  Then everything you removed from the Model has to be rewritten in the script directly using python code that does the equivalent process that the ModelBuilder Iterator did originally in ModelBuilder.

Iteration (looping and branching) is much more sophisticated in Python than it is ModelBuilder.  Python variables, cursors, lists, dictionaries, conditional checks and looping possibilities in a script are much more powerful and elegant than a ModelBuilder Iterator.  So ultimately Python can do everything ModelBuilder does with an Iterator or variable better than ModelBuilder can and many things ModelBuilder cannot do can be done in Python.  But you have to learn the python way of doing iterations and forget about the ModelBuider way of doing Iterations if your ultimate goal is to run a python script.
0 Kudos
RichardFairhurst
MVP Honored Contributor
If a tool name exported from modebuilder that has "_mb" in the tool name that tool is a ModelBuilder specific tool.  Any exported tool with that in the tool name has to be removed from an exported Python script and rewritten.

The Select_analysis processes exported from your model may work, but it is possible that these processes also need to be rewritten in the Python script to replace them with real python parameters/variables and conditional branching code if the Select_analysis processes were used by ModelBuilder to do branching.  I have never used that tool and don't know where it came from, so I don't know if it is ModelBuilder specific or not.

The Python script can retain the Feature Vertices to Points, Spatial Join and Merge tools, because these came from the geoprocessing toolboxes and do not have "_mb" in the name.
0 Kudos
tamerali
New Contributor III
thank you Richard for your answer , i make python script it run ok from gis python environment , but whe i export it as python script, it don't return the true as gis environment , if any idea ?

here is :

import arcpy
arcpy.env.workspace= 'E:\\cad.mdb'
scale= '12500'
cadf= 'E:\\cad_distace.DWG'
datsef = 'cad'
datsef = 'cad'
try:
    arcpy.CADToGeodatabase_conversion(cadf,gdbf,datsef,scale)
    if arcpy.Exists('E:\\cad.mdb\\cad\\polyline'):
       arcpy.Select_analysis('E:\\cad.mdb\\cad\\polyline','E:\\cad.mdb\\south',"Layer ='ج�?�?ب'")
except:     
    print 'lolo'
try:   
      arcpy.FeatureVerticesToPoints_management('E:\\cad.mdb\\cad\\polyline','E:\\cad.mdb\\points',"ALL")
except:
    print 'ggg'
   

... it return the exception (lolo,ggg) >
0 Kudos
RichardFairhurst
MVP Honored Contributor
thank you Richard for your answer , i make python script it run ok from gis python environment , but whe i export it as python script, it don't return the true as gis environment , if any idea ?

here is :

import arcpy
arcpy.env.workspace= 'E:\\cad.mdb'
scale= '12500'
cadf= 'E:\\cad_distace.DWG'
datsef = 'cad'
datsef = 'cad'
scale= '12500'
try:
    if arcpy.Exists('E:\\cad.mdb\\cad\\polyline'):
    arcpy.CADToGeodatabase_conversion(cadf,gdbf,datsef,scale)
    if arcpy.Exists('E:\\cad.mdb\\cad\\polyline'):
       arcpy.Select_analysis('E:\\cad.mdb\\cad\\polyline','E:\\cad.mdb\\south',"Layer ='ج�?�?ب'")
except:      
    print 'lolo'
try:    
      arcpy.FeatureVerticesToPoints_management('E:\\cad.mdb\\cad\\polyline','E:\\cad.mdb\\points',"ALL")
except:
    print 'ggg'

   

... it return the exception (lolo,ggg) >


The Overwrite output option is ignored outside of ArcMap.  You probably ran the model once already and are trying to run it again and the error is due to the fact that the tool won't overwrite the feature class any more.  Your best option is to add another Exists test for the output and if it exists use the Delete_management tool to delete it prior to running the any tool with an output.  I think you also meant to set the gdbf variable but set a duplicate of the datsef variable instead.  So I think this will work:

import arcpy
arcpy.env.workspace= 'E:\\cad.mdb'
cadf= 'E:\\cad_distace.DWG'
gdbf = 'E:\\cad.mdb'
datsef = 'cad'
scale= '12500'
try:
    if arcpy.Exists('E:\\cad.mdb\\cad'):
        arcpy.Delete_management("E:\\cad.mdb\\cad")
    arcpy.CADToGeodatabase_conversion(cadf,gdbf,datsef,scale)
    if arcpy.Exists('E:\\cad.mdb\\cad\\polyline'):
        if arcpy.Exists('E:\\cad.mdb\\south'):
            arcpy.Delete_management("E:\\cad.mdb\\south")
        arcpy.Select_analysis('E:\\cad.mdb\\cad\\polyline','E:\\cad.mdb\\south',"Layer ='ج�?�?ب'")
except:      
    print 'lolo'
try:    
    if arcpy.Exists('E:\\cad.mdb\\points'):
        arcpy.Delete_management("E:\\cad.mdb\\points")
    arcpy.FeatureVerticesToPoints_management('E:\\cad.mdb\\cad\\polyline','E:\\cad.mdb\\points',"ALL")
except:
    print 'ggg'


Once you get this to work you could make this an inner loop of an os.walk directory read operation that reads all of the cad files of a directory and uses variables instead of hard value outputs to adjust to each new output to match the input cad name to create separate cad file conversion feature classes and polyline and/or point feature classes in the same mdb (or gdb).
RichardFairhurst
MVP Honored Contributor
By the way, if you have found my posts or any other user's posts helpful please read this link:  Assigning points and marking answers
0 Kudos
RichardFairhurst
MVP Honored Contributor

The link was to the old forum MVP page.  It does not exist any longer.  As best I can tell, all you can do is Like a post now.

I don't know when my MVP points from the old forum will appear or how liking posts in this forum will affect the MVP standings yet.  It appears they are still trying to bring over MVP data from the old site before they will define how a new MVP program will work here.