In day to day tasks, we often use ArcGIS Pro to carry out many processes and run many geoprocessing tools to manage our content and keep it up to date. For example, you could spend a few hours weekly on updating the information/data on your Web Server such as the content of your ArcGIS Online (organizational)-account. However, this is quite time-consuming to spend weekly on busy work. Python API is a powerful tool that helps you create your own geoprocessing tool that runs a list of executables. Even better, you can schedule the task to run the pipeline as frequently as you wish. This blog will show you how to convert a standalone Python script into a Python script toolbox and schedule it on ArcGIS Pro. Therefore, I will use a script created earlier which can be downloaded (together with the script-toolbox derived from it in this blog) from this GitHub repository. Also, if you are interested, this StoryMap explains and walks you through the steps of creating the script and automating the publishing of Revit Model to Scene Layer - the story map explains how to use Python API for automating the workflow as a standalone script. As a continuation, this blog will explain how to create a geoprocessing tool (script tool) that executes the same workflow using Python.
The flowchart above shows the entire workflow or publishing a Revit model to scene layer. In the script, we have divided this workflow into two parts that we will be calling functions.
Function 1
def CreateBSLpackage():
Function 2
def publishBSLfunction():
In the remainder of this blog we will:
Requirements for following the tutorial:
Before starting setting up the development environment, it might be useful to watch this awesome tutorial on how to create a script tool. It walks you through the same steps for a small script!
The following dialog box pops up:
Keep the Import script checkbox unchecked during the developing phase for testing purposes. After you finish the script and you are content with it, you can check the checkbox for embedding the script in the toolbox, help - Add a script tool.
def CreateBSLpackage(workSpaceEnv = None, GDBfolder_name = r"AutomationGDB.gdb",\
out_FeatureDataset= r"Building_A" , spatial_reference = r"RD New", \
Rvt_directory = None , BSL_name = r"BSLpackage.slpk",\
nameOfBuildingL = r"BuildL_A" , includeDate = False):
if __name__ == '__main__':
run_CreateBSLpackage = True
"""required parameters and Optional parameters for the workflow
this workflow is devided into two main parts (here functions)
#################################
###Function 1 CreateBSLpackage()#
#################################
"""
# Required
##########
workSpaceEnv = r"C:\Users\alhoz\Desktop\AutomationTest" # path to a folder to createGDB
Rvt_directory = r"C:\Users\alhoz\Desktop\AutomationTest\revitFiles\171025_BLOKA.rvt" # path to rvt file
BSL_name = r"BSLpackageBlockNew.slpk" # BSLpackage Name defaul is **BSLpackage.slpk**
spatial_reference = r"RD New" #default is "RD New"
nameOfBuildingL = r"BuildL_Anew" #this will show on the ArcGIS online
# optional
##########
out_FeatureDataset= r"Building_A"
GDBfolder_name = r"AutomationTESTnew.gdb" #default
includeDate = False
run_CreateBSLpackage = arcpy.GetParameterAsText(0)
workSpaceEnv = arcpy.GetParameterAsText(1)
Rvt_directory = arcpy.GetParameterAsText(2)
At this stage, the full script below takes as input for the three arguments from the wizard of the geoprocessing tool. In the next step, we will make sure that the user passes on the right input through the geoprocessing tool interface.
if __name__ == '__main__':
run_CreateBSLpackage = arcpy.GetParameterAsText(0)
"""required parameters and Optional parameters for the workflow
this workflow is devided into two main parts (here functions)
#################################
###Function 1 CreateBSLpackage()#
#################################
"""
# Required
##########
workSpaceEnv = arcpy.GetParameterAsText(1)
Rvt_directory = arcpy.GetParameterAsText(2) # path to rvt file
BSL_name = r"BSLpackageBlockNew.slpk" # BSLpackage Name defaul is **BSLpackage.slpk**
spatial_reference = r"RD New" #default is "RD New"
nameOfBuildingL = r"BuildL_Anew" #this will show on the ArcGIS online
# optional
##########
out_FeatureDataset= r"Building_A"
GDBfolder_name = r"AutomationTESTnew.gdb" #default
includeDate = False
Once you've finished defining all parameters, and if you have worked on the same script; the tool should look similar to the snippet below. Now you can define the parameters and automate the process using the scheduler.
Here are the final parameters' properties:
if __name__ == '__main__':
run_CreateBSLpackage = True
run_publishBSLfunction= True
checkDateOfRevitFile = True # only works if you add (TimesLog.txt) file and the directory to it
###########################################################################################################
#directry to the text file in which the histoy log is stored and used for the date check of revit files####
## see helper function:)
directoryToTXTfile = r"C:\Users\alhoz\Desktop\Automation\FinalPythonCodes\TimesLog.txt"
"""required parameters and Optional parameters for the workflow
this workflow is devided into two main parts (here functions)
#################################
###Function 1 CreateBSLpackage()#
#################################
"""
# Required
##########
workSpaceEnv = r"C:\Users\alhoz\Desktop\AutomationTest" # path to a folder to createGDB
Rvt_directory = r"C:\Users\alhoz\Desktop\AutomationTest\revitFiles\171025_BLOKA.rvt" # path to rvt file
BSL_name = r"BSLpackageBlockNew.slpk" # BSLpackage Name defaul is **BSLpackage.slpk**
spatial_reference = r"RD New" #default is "RD New"
nameOfBuildingL = r"BuildL_Anew" #this will show on the ArcGIS online
# optional
##########
out_FeatureDataset= r"Building_A"
GDBfolder_name = r"AutomationTESTnew.gdb" #default
includeDate = False
####################################
# check date if True and run the Function :CreateBSLpackage
if checkDateOfRevitFile and run_CreateBSLpackage:
run_CreateBSLpackage = checkDateFunction(Rvt_directory,directoryToTXTfile)
if not run_CreateBSLpackage:
run_publishBSLfunction = False
print ("The last version of the Revit file is already uploaded")
if run_CreateBSLpackage:
CreateBSLpackage(workSpaceEnv, GDBfolder_name , \
out_FeatureDataset, spatial_reference , Rvt_directory, BSL_name,\
nameOfBuildingL , includeDate )
#####################################################################
#####################################################################
#####################################################################
""" ###################################
###Function 2 publishBSLfunction()#
###################################
"""
# Required
##########
itemID_BSLp = "a3826c603e0d4f7d8c2a79ea35a88465"
itemID_Hosted = "0af1fd92f95d46768a72b59845175bae"
# overwrite parameter is importatnt to be set on **True** ##Only change if you know what you are doing##
dictOfPackageLayer = {"overwrite" : True}
DirectoryTo_SLPK = None
#### this parameter is automaticlly derived from the arguments of function 1
#### do not change it unless you know what you are doing
if includeDate:
now = datetime.now()
day = now.strftime("%Y%m%d")
BSL_name = BSL_name[:-5] +day + BSL_name[-5:]
if DirectoryTo_SLPK is None:
DirectoryTo_SLPK = workSpaceEnv + "\\" + BSL_name
if run_publishBSLfunction:
publishBSLfunction(itemID_BSLp, itemID_Hosted, dictOfPackageLayer, DirectoryTo_SLPK)
if __name__ == '__main__':
run_CreateBSLpackage = arcpy.GetParameterAsText(0)
if run_CreateBSLpackage == "true":
run_CreateBSLpackage = True
else:
run_CreateBSLpackage = False
run_publishBSLfunction= arcpy.GetParameterAsText(9)
if run_publishBSLfunction == "true":
run_publishBSLfunction = True
else:
run_publishBSLfunction = False
checkDateOfRevitFile = arcpy.GetParameterAsText(13) # only works if you add (TimesLog.txt) file and the directory to it
if checkDateOfRevitFile == "true":
checkDateOfRevitFile = True
else:
checkDateOfRevitFile = False
###########################################################################################################
#directry to the text file in which the histoy log is stored and used for the date check of revit files####
## see helper function:)
directoryToTXTfile = arcpy.GetParameterAsText(14)
"""required parameters and Optional parameters for the workflow
this workflow is devided into two main parts (here functions)
#################################
###Function 1 CreateBSLpackage()#
#################################
"""
# Required
##########
workSpaceEnv = arcpy.GetParameterAsText(1) # path to a folder to createGDB
Rvt_directory = arcpy.GetParameterAsText(2) # path to rvt file
BSL_name = arcpy.GetParameterAsText(3) + ".slpk" # BSLpackage Name defaul is **BSLpackage.slpk**
spatial_reference = arcpy.GetParameterAsText(4) #default is "RD New"
nameOfBuildingL = arcpy.GetParameterAsText(5) #this will show on the ArcGIS online
# optional
##########
out_FeatureDataset= arcpy.GetParameterAsText(6)
GDBfolder_name = arcpy.GetParameterAsText(7) + ".gdb" #default
includeDate = arcpy.GetParameterAsText(8)
if includeDate == "true":includeDate = True
else:includeDate = False
####################################
# check date if True and run the Function :CreateBSLpackage
if checkDateOfRevitFile and run_CreateBSLpackage:
run_CreateBSLpackage = checkDateFunction(Rvt_directory,directoryToTXTfile)
if not run_CreateBSLpackage:
run_publishBSLfunction = False
arcpy.AddMessage ("The last version of the Revit file is already uploaded")
if run_CreateBSLpackage:
CreateBSLpackage(workSpaceEnv, GDBfolder_name , \
out_FeatureDataset, spatial_reference , Rvt_directory, BSL_name,\
nameOfBuildingL , includeDate )
#####################################################################
#####################################################################
#####################################################################
""" ###################################
###Function 2 publishBSLfunction()#
###################################
"""
# Required
##########
itemID_BSLp = arcpy.GetParameterAsText(10)
if itemID_BSLp =="": itemID_BSLp = None
itemID_Hosted = arcpy.GetParameterAsText(11)
if itemID_Hosted=="": itemID_Hosted= None
# overwrite parameter is importatnt to be set on **True** ##Only change if you know what you are doing##
dictOfPackageLayer = {"overwrite" : True}
DirectoryTo_SLPK = arcpy.GetParameterAsText(12)
#### this parameter is automaticlly derived from the arguments of function 1
#### do not change it unless you know what you are doing
if includeDate:
now = datetime.now()
day = now.strftime("%Y%m%d")
BSL_name = BSL_name[:-5] +day + BSL_name[-5:]
if DirectoryTo_SLPK == "":
DirectoryTo_SLPK = workSpaceEnv + "\\" + BSL_name
if run_publishBSLfunction:
publishBSLfunction(itemID_BSLp, itemID_Hosted, dictOfPackageLayer, DirectoryTo_SLPK)
Let's take the same boolean input from example one:
run_CreateBSLpackage = arcpy.GetParameterAsText(0)
if run_CreateBSLpackage == "true":
run_CreateBSLpackage = True
else:
run_CreateBSLpackage = False
The wizard passes on the value in type string to the script, therefore, Python does not recognize Boolean (True of False) value. It simply read as a word true. There is a way around it, which is to simply change the value to Boolean, with if statement, as shown in the example above.
If you check the rest of the input parameters, you will find examples of dealing with other types of input from the wizard. Script (before and after) and the toolbox BIMpublicationtoolbox can be found in the attachments, for if you wish to develop them or use them directly. Note that the same files, which will probably be improved in the future, can be downloaded from the GitHub repo.
For any questions about this blog or any other questions, feel free to reach out.
My name is Khaled Alhoz, an intern at Esri Nederland graduating from a master program in Geomatics at Tu Delft university in Delft, The Nederlands. My work focuses on GIS&BIM-integration related topics.
Contact info:
Khaled Alhoz
GIS-BIM intern at Esri Nederland
LinkedIn
Emails:
kalhoz@esri.nl
k.alhoz.geo@gmail.com,
My contribution are to fine on GitHub:
GitHub repositories
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.