Removing doubles on imported 3DXML ngon mesh Pertaining to building objects created from the script shown in the answer here https://blender.stackexchange.com/a/42590/15543  



The import data has seperate vert data for each surface, so each face can be selected and moved ( as shown in image ) which produces a lot of doubles. For example a simple cube would have 6 x 4 verts which after remove doubles would be reduced to 8.

Each surface has uv texture coordinates.



To vastly reduce the amount of verts in the scene I thought I would remove doubles.  However,  running the bpy.ops or bmesh.ops remove doubles also removes faces such as the one highlighted, leaving gaping holes in buildings.

Any suggestions on how to approach removing doubles on such meshes leaving faces intact, which can be ultimately be scripted?

If the poly is opened up like shown then remove doubles works well. If remove doubles is run on the separated but "closed" poly then remove doubles fills the holes with edges.
EDIT

A better approach would be to look for the two "ripped" edges in a face that share the same geometry. (edges parallel with same mid point test or similar), and remove the edges verts from the remove doubles list.



Thanks for your help guys, The answer kinda came to me when I was creating pics to explain the situation and opened up the poly.  

The trick was looking at faces with more than one double and removing them from the remove doubles operator, otherwise will end up trying to make a poly with holes, which ends up being a hole.

The following reduces vert count of building.014 from 4205 to 1258... and leaves the roof intact.

import bpy
import bmesh
context = bpy.context
obj = context.object
print("X" * 80)
bm = bmesh.new()
bm.from_mesh(obj.data)
bm.faces.ensure_lookup_table()
ignore_doubles = []
for f in bm.faces:
    verts = f.verts
    dubs = bmesh.ops.find_doubles(bm,
                                  verts=verts,
                                  dist=0.00001)
    double_verts = dubs['targetmap']
    if len(double_verts.keys()) &gt; 1:
        for k, v in double_verts.items():
            ignore_doubles.append(v)

verts = list(set(bm.verts) - set(ignore_doubles))            
bmesh.ops.remove_doubles(bm, verts=verts, dist=0.00001)

bm.to_mesh(context.object.data)