Select to view content in your preferred language

How to access multiple shp files in a folder "selet objects"->"Shapes"->"union shapes" in sequence

1773
9
12-13-2022 01:51 AM
hoya
by
Emerging Contributor

I am curious about the script that sequentially loads multiple shp files in the folder, selects the upper layer as shown in picture 1, selects "selet objects", accesses the top menu "Shapes" as shown in picture 2, and executes "union shapes"

( picture 1)

morai_0-1670924908370.png

( picture 2)

morai_1-1670924939699.png

 

0 Kudos
9 Replies
plfontes
Regular Contributor

Hi @hoya ,

This script should do what you described. It assumes though that:

  • the SHP files are within your project's folder
  • the SHP files are numerically sequential
  • your scene is empty before running the script

you need to change these variables to fit your project:

initialFileNumber
finalFileNumber
fileLocation
fileNameRoot

Here's the script:

'''
Created on Dec 13, 2022

@author: Plfontes
'''
from scripting import *

# get a CityEngine instance
ce = CE()

initialFileNumber = 1 # the lowest/first number after the immutable part of the filename  from all your SHP files
finalFileNumber = 5 # the last number after the immutable part of the filename from all your SHP files
fileLocation = "path/to/your/SHP/files/" # where the SHP file are locate on your CE project folders
fileNameRoot = "rootPartOfYourFileName" # the first part of your SHP file list that is immutable

# import the SHP files into the scene
def importSHPStack():
    i = initialFileNumber
    while i <= finalFileNumber:
        settings = SHPImportSettings()
        ce.importFile(ce.toFSPath(fileLocation+fileNameRoot+str(i)+".shp"), settings)
        i += 1
        
# run the union shape command
def unionShapes():
    objects = ce.getObjectsFrom(ce.scene, ce.isShape)
    for o in objects:
        shape = ce.unionShapes(objects)
        
def deleteEmptyShapeLayers():
    shapeLayers = ce.getObjectsFrom(ce.scene, ce.isShapeLayer)
    for s in shapeLayers:
        if len(ce.getObjectsFrom(s, ce.isShape)) == 0:
            ce.delete(s)

if __name__ == '__main__':
    importSHPStack()
    unionShapes()
    deleteEmptyShapeLayers()

 

Hope this helps.

 

0 Kudos
hoya
by
Emerging Contributor

morai_0-1670984445077.png

First of all, thank you very much for your answer. If there are shp files in the path of the attached picture, how should the script be written?

0 Kudos
plfontes
Regular Contributor

Hi @hoya ,

The script amended to fit the above screeshot would be like this:

'''
Created on Dec 13, 2022

@author: Plfontes
'''
from scripting import *

# get a CityEngine instance
ce = CE()

initialFileNumber = 3466 # the lowest/first number after the immutable part of the filename  from all your SHP files
finalFileNumber = 4799 # the last number after the immutable part of the filename from all your SHP files
fileLocation = "data/" # where the SHP file are locate on your CE project folders
fileNameRoot = "52SBB" # the first part of your SHP file list that is immutable

# import the SHP files into the scene
def importSHPStack():
    i = initialFileNumber
    while i <= finalFileNumber:
        settings = SHPImportSettings()
        ce.importFile(ce.toFSPath(fileLocation+fileNameRoot+str(i)+".shp"), settings)
        i += 1
        
# run the union shape command
def unionShapes():
    objects = ce.getObjectsFrom(ce.scene, ce.isShape)
    for o in objects:
        shape = ce.unionShapes(objects)
        
def deleteEmptyShapeLayers():
    shapeLayers = ce.getObjectsFrom(ce.scene, ce.isShapeLayer)
    for s in shapeLayers:
        if len(ce.getObjectsFrom(s, ce.isShape)) == 0:
            ce.delete(s)

if __name__ == '__main__':
    importSHPStack()
    unionShapes()
    deleteEmptyShapeLayers()

 

0 Kudos
hoya
by
Emerging Contributor

First of all, thank you very much for your answer. I ran the script, but only the first file is loaded in "3D View" and "Select Object" and "Combined Shape" do not work. Attached is the shp file I'm working on.

0 Kudos
plfontes
Regular Contributor

Hi @hoya ,

I had issues trying to load your data. One of the SHP files did not load. In any case, I've simplified the code to load any SHP file from the designated data folder.

See if it work on your machine. Here's the updated script:

 

'''
Created on Dec 13, 2022

@author: Plfontes
'''
from scripting import *

# get a CityEngine instance
ce = CE()

fileLocation = "data/" # where the SHP file are locate on your CE project folders

# import the SHP files into the scene
def importSHPStack():
    shpFiles = ce.getObjectsFrom(fileLocation, ce.isFile)
    shpFilesFiltered = list(filter(lambda x: x.endswith('shp'), shpFiles))
    for shpFile in shpFilesFiltered:
        settings = SHPImportSettings()
        ce.importFile(ce.toFSPath(shpFile), settings)
                
# run the union shape command
def unionShapes():
    objects = ce.getObjectsFrom(ce.scene, ce.isShape)
    for o in objects:
        shape = ce.unionShapes(objects)
        
def deleteEmptyShapeLayers():
    shapeLayers = ce.getObjectsFrom(ce.scene, ce.isShapeLayer)
    for s in shapeLayers:
        if len(ce.getObjectsFrom(s, ce.isShape)) == 0:
            ce.delete(s)

if __name__ == '__main__':
    importSHPStack()
    unionShapes()
    deleteEmptyShapeLayers()

 

Cheers, 

0 Kudos
hoya
by
Emerging Contributor

First of all thank you so much for your reply. However, when I run the current script, the modeling is configured the same as in "Picture 1", and "CityEngnie" continues to work and the script does not end. But what I want is to run "select objects"->"union shapes" for each modeling like "Picture 2".

( picture 1)

morai_1-1671153944900.png

( picture 2)

morai_0-1671153571237.png

 

0 Kudos
plfontes
Regular Contributor

Hi @hoya ,

I believe there is an issues with one of your SHP files, as I mentioned before. The script gets stuck trying to union the imported shapes, which may be an indicative of problems with the geometries. Can you load the files individually without any problem?

Best

0 Kudos
hoya
by
Emerging Contributor

Thank you so much for replying first.

I deleted the "shp" identified as the problem and ran the script again, but the same problem as before occurs. If I do it manually without using a script, it runs without problems, but I have to run it as a script because I need to run a lot of files. Edit the file again and attach it. Check it out.

0 Kudos
plfontes
Regular Contributor

Hi @hoya ,

Here's the fix.

'''
Created on Dec 13, 2022

@author: Plfontes
'''
from scripting import *

# get a CityEngine instance
ce = CE()

fileLocation = "data/" # where the SHP file are locate on your CE project folders

# import the SHP files into the scene
def importSHPStack():
    shpFiles = ce.getObjectsFrom(fileLocation, ce.isFile)
    shpFilesFiltered = list(filter(lambda x: x.endswith('shp'), shpFiles))
    for shpFile in shpFilesFiltered:
        settings = SHPImportSettings()
        ce.importFile(ce.toFSPath(shpFile), settings)
                
# run the union shape command
def unionShapes():
    objects = ce.getObjectsFrom(ce.scene, ce.isShape)
    ce.unionShapes(objects)
        
def deleteEmptyShapeLayers():
    shapeLayers = ce.getObjectsFrom(ce.scene, ce.isShapeLayer)
    for s in shapeLayers:
        if len(ce.getObjectsFrom(s, ce.isShape)) == 0:
            ce.delete(s)

if __name__ == '__main__':
    importSHPStack()
    unionShapes()
    deleteEmptyShapeLayers()

It should perform well now.

Cheers.
Patrick