mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-09-25 17:57:02 +00:00
Throw in experimental import and bulk rename script
This commit is contained in:
@@ -0,0 +1,115 @@
|
|||||||
|
import bpy
|
||||||
|
from bpy.types import Operator
|
||||||
|
|
||||||
|
class Object_OT_RenameObjects(Operator):
|
||||||
|
bl_idname = "object.renameobjects"
|
||||||
|
bl_label = "Rename Object(s)"
|
||||||
|
|
||||||
|
# Multi Object rename UI
|
||||||
|
BNameCB : bpy.props.BoolProperty(name = "Base Name:")
|
||||||
|
BaseName : bpy.props.StringProperty(name = "")
|
||||||
|
PreFixCB : bpy.props.BoolProperty(name = "Prefix:")
|
||||||
|
PreFix : bpy.props.StringProperty(name = "")
|
||||||
|
RemFrst : bpy.props.BoolProperty(name = "Remove First")
|
||||||
|
DgtFrst : bpy.props.IntProperty(name = "Digits")
|
||||||
|
SuffixCB : bpy.props.BoolProperty(name = "Suffix")
|
||||||
|
Suffix : bpy.props.StringProperty(name = "")
|
||||||
|
RemLast : bpy.props.BoolProperty(name = "Remove Last")
|
||||||
|
DgtLast : bpy.props.IntProperty(name = "Digits")
|
||||||
|
NumbredCB : bpy.props.BoolProperty(name = "Numbred")
|
||||||
|
BaseNum : bpy.props.IntProperty(name = "Base Number")
|
||||||
|
Step : bpy.props.IntProperty(name = "Step", default = 1)
|
||||||
|
findCB : bpy.props.BoolProperty(name = "Replace")
|
||||||
|
find : bpy.props.StringProperty(name = "")
|
||||||
|
replace : bpy.props.StringProperty(name = "")
|
||||||
|
# Single rename UI
|
||||||
|
Name : bpy.props.StringProperty(name="Name")
|
||||||
|
|
||||||
|
def draw(self, ctx):
|
||||||
|
SelCount = len(bpy.context.selected_objects)
|
||||||
|
if SelCount > 1:
|
||||||
|
box = self.layout.box()
|
||||||
|
row = box.row()
|
||||||
|
row.prop(self, "BNameCB")
|
||||||
|
row.prop(self, "BaseName")
|
||||||
|
row = box.row()
|
||||||
|
row.prop(self, "PreFixCB")
|
||||||
|
row.prop(self, "PreFix")
|
||||||
|
row = box.row()
|
||||||
|
row.prop(self, "RemFrst")
|
||||||
|
row.prop(self, "DgtFrst")
|
||||||
|
row = box.row()
|
||||||
|
row.prop(self, "SuffixCB")
|
||||||
|
row.prop(self, "Suffix")
|
||||||
|
row = box.row()
|
||||||
|
row.prop(self, "RemLast")
|
||||||
|
row.prop(self, "DgtLast")
|
||||||
|
row = box.row(align=True)
|
||||||
|
row.prop(self, "NumbredCB")
|
||||||
|
row.prop(self, "BaseNum")
|
||||||
|
row.prop(self, "Step")
|
||||||
|
row = box.row(align=True)
|
||||||
|
row.prop(self, "findCB")
|
||||||
|
row.prop(self, "find")
|
||||||
|
row.prop(self, "replace")
|
||||||
|
if SelCount == 1:
|
||||||
|
box = self.layout.box()
|
||||||
|
row = box.row()
|
||||||
|
row.prop(self, "Name")
|
||||||
|
if SelCount == 0:
|
||||||
|
box = self.layout.box()
|
||||||
|
row = box.row()
|
||||||
|
row.label("No Selected Object")
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
if len(bpy.context.selected_objects) == 1:
|
||||||
|
self.Name = bpy.context.selected_objects[0].name
|
||||||
|
|
||||||
|
def execute(self, context):
|
||||||
|
SelCount = len(bpy.context.selected_objects)
|
||||||
|
if SelCount > 1:
|
||||||
|
SelObj = bpy.context.selected_objects
|
||||||
|
Index = self.BaseNum
|
||||||
|
for i in range(0,SelCount):
|
||||||
|
# Get Object Original Name #
|
||||||
|
NewName = SelObj[i].name
|
||||||
|
# Set the Base name #
|
||||||
|
if self.BNameCB:
|
||||||
|
NewName = self.BaseName
|
||||||
|
# Remove First characters #
|
||||||
|
if self.RemFrst:
|
||||||
|
NewName = NewName[self.DgtFrst : self.DgtFrst + len(NewName)]
|
||||||
|
# Remove Last Characters #
|
||||||
|
if self.RemLast:
|
||||||
|
NewName = NewName[1 : len(NewName) - self.DgtLast]
|
||||||
|
# Add Prefix to the new name #
|
||||||
|
if self.PreFixCB:
|
||||||
|
NewName = self.PreFix + NewName
|
||||||
|
# Add Suffix to the new name #
|
||||||
|
if self.SuffixCB:
|
||||||
|
NewName = NewName + self.Suffix
|
||||||
|
# Add Digits to end of new name #
|
||||||
|
if self.NumbredCB:
|
||||||
|
NewName += str(Index)
|
||||||
|
Index += self.Step
|
||||||
|
# Find and Replace #
|
||||||
|
if self.findCB:
|
||||||
|
NewName = NewName.replace(self.find, self.replace)
|
||||||
|
# Set the new name to the object #
|
||||||
|
SelObj[i].name = NewName
|
||||||
|
elif SelCount == 1:
|
||||||
|
bpy.context.selected_objects[0].name = self.Name
|
||||||
|
return {'FINISHED'}
|
||||||
|
|
||||||
|
def invoke(self, context, event):
|
||||||
|
wm = context.window_manager
|
||||||
|
return wm.invoke_props_dialog(self)
|
||||||
|
|
||||||
|
def register():
|
||||||
|
bpy.utils.register_class(Object_OT_RenameObjects)
|
||||||
|
|
||||||
|
def unregister():
|
||||||
|
bpy.utils.unregister_class(Object_OT_RenameObjects)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
register()
|
||||||
@@ -0,0 +1,84 @@
|
|||||||
|
#import sys
|
||||||
|
#sys.path.append('C:/Program Files/Python37/Lib/site-packages')
|
||||||
|
import ifcopenshell
|
||||||
|
import ifcopenshell.geom
|
||||||
|
import bpy
|
||||||
|
import time
|
||||||
|
import mathutils
|
||||||
|
|
||||||
|
class ImportIFC():
|
||||||
|
def __init__(self):
|
||||||
|
self.file = None
|
||||||
|
self.settings = ifcopenshell.geom.settings()
|
||||||
|
self.meshes = {}
|
||||||
|
|
||||||
|
def execute(self):
|
||||||
|
self.load_file()
|
||||||
|
n = 0
|
||||||
|
elements = self.file.by_type('IfcElement')
|
||||||
|
for element in elements:
|
||||||
|
self.create_object(element)
|
||||||
|
n += 1
|
||||||
|
if n > 1000:
|
||||||
|
break
|
||||||
|
|
||||||
|
def load_file(self):
|
||||||
|
self.file = ifcopenshell.open('C:/cygwin64/home/moud308/Projects/IFCAudit/big.ifc')
|
||||||
|
|
||||||
|
def create_object(self, element):
|
||||||
|
try:
|
||||||
|
if element.is_a('IfcOpeningElement'):
|
||||||
|
return
|
||||||
|
|
||||||
|
shape = ifcopenshell.geom.create_shape(self.settings, element)
|
||||||
|
mesh_name = 'mesh-{}'.format(shape.geometry.id)
|
||||||
|
|
||||||
|
mesh = self.meshes.get(mesh_name)
|
||||||
|
if mesh is None:
|
||||||
|
mesh = self.create_mesh(element, shape)
|
||||||
|
self.meshes[mesh_name] = mesh
|
||||||
|
else:
|
||||||
|
print('we reused a mesh!')
|
||||||
|
|
||||||
|
materials = shape.geometry.materials
|
||||||
|
for material in materials:
|
||||||
|
if material.has_diffuse:
|
||||||
|
alpha = 1.
|
||||||
|
blender_material = bpy.data.materials.new('mymaterial')
|
||||||
|
blender_material.diffuse_color = material.diffuse + (alpha,)
|
||||||
|
mesh.materials.append(blender_material)
|
||||||
|
|
||||||
|
object = bpy.data.objects.new('{}/{}'.format(element.is_a(), element.Name), mesh)
|
||||||
|
m = shape.transformation.matrix.data
|
||||||
|
matrix = mathutils.Matrix((
|
||||||
|
[m[0], m[1], m[2], 0],
|
||||||
|
[m[3], m[4], m[5], 0],
|
||||||
|
[m[6], m[7], m[8], 0],
|
||||||
|
[m[9], m[10], m[11], 1]))
|
||||||
|
matrix.transpose()
|
||||||
|
object.matrix_world = matrix
|
||||||
|
bpy.context.scene.collection.objects.link(object)
|
||||||
|
except:
|
||||||
|
print('Could not create object for {}: {}/{}'.format(
|
||||||
|
element.GlobalId, element.is_a(), element.Name))
|
||||||
|
|
||||||
|
def create_mesh(self, element, shape):
|
||||||
|
try:
|
||||||
|
mesh = bpy.data.meshes.new(shape.geometry.id)
|
||||||
|
f = shape.geometry.faces
|
||||||
|
v = shape.geometry.verts
|
||||||
|
vertices = [[v[i], v[i + 1], v[i + 2]]
|
||||||
|
for i in range(0, len(v), 3)]
|
||||||
|
faces = [[f[i], f[i + 1], f[i + 2]]
|
||||||
|
for i in range(0, len(f), 3)]
|
||||||
|
mesh.from_pydata(vertices, [], faces)
|
||||||
|
return mesh
|
||||||
|
except:
|
||||||
|
print('Could not create mesh for {}: {}/{}'.format(
|
||||||
|
element.GlobalId, element.is_a(), element.Name))
|
||||||
|
|
||||||
|
print('# Starting import')
|
||||||
|
start = time.time()
|
||||||
|
import_ifc = ImportIFC()
|
||||||
|
import_ifc.execute()
|
||||||
|
print('# Finished in {}s'.format(time.time() - start))
|
||||||
Reference in New Issue
Block a user