How can we delete existing parts , and import a new geometry ?

Erik Kostson
Erik Kostson Member, Moderator, Employee Posts: 275
50 Answers 100 Comments Second Anniversary 25 Likes
✭✭✭✭
edited August 6 in Structures

Say we have an existing geometry with parts, how can we first delete it and then finally import a new geometry into mechanical?

Best Answer

  • Erik Kostson
    Erik Kostson Member, Moderator, Employee Posts: 275
    50 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭
    edited August 6 Answer ✓

    One way is as described below (works in 2023 R2 and 2024 R2):

    import os
    
    work_dir = r'D:\myheatfiles\PyAnsys\PyMechanical\demo' ## geometry folder / change as needed
    
    ## Import geometry
    
    geometry_file = os.path.join(work_dir,'Valve.scdoc')
    
    geometry_import=ExtAPI.DataModel.Project.Model.GeometryImportGroup[0] # gets the Geometry Import
    
    geopartdel=[] ## parts to be deleted
    ##Delete existing parts
    
    for assembly in ExtAPI.DataModel.GeoData.Assemblies:
        for part in assembly.Parts:
            for body in part.Bodies:
                geopartdel.Add(body.Part)
    
    Model.DeleteParts(geopartdel)
    
    ##Imports new parts
    
    geometry_import.Import(geometry_file)
    

    or without looping (parts):

    import os
    
    work_dir = r'D:\demo' 
    
    
    geometry_file = os.path.join(work_dir,'Valve.scdoc')
    
    geometry_import=ExtAPI.DataModel.Project.Model.GeometryImportGroup[0]
    
    
    geopartdel=ExtAPI.DataModel.GeoData.Assemblies[0].AllParts
    
    Model.DeleteParts(geopartdel)
    
    
    geometry_import.Import(geometry_file)
    
This discussion has been closed.