-
Notifications
You must be signed in to change notification settings - Fork 15
/
importerIL.py
149 lines (136 loc) · 4.35 KB
/
importerIL.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# -*- coding: utf-8 -*-
'''
importer.py:
Collection of 3D Mesh importers
'''
import os, sys, FreeCAD, FreeCADGui, importerSAT, importerDXF, Import_IPT, importerF3D
import Acis, importerClasses
from importerUtils import canImport, logInfo, logWarning, logError, logAlways, getAuthor, getComment, getLastModifiedBy, setThumbnail
from olefile import isOleFile
from importerFreeCAD import createGroup
from pivy import coin
__author__ = "Jens M. Plonka"
__copyright__ = 'Copyright 2018, Germany'
__url__ = "https://www.github.com/jmplonka/InventorLoader"
def decode(name):
"decodes encoded strings"
decodedName = name
try:
decodedName = name.encode(sys.getfilesystemencoding()).decode("utf8")
except:
logWarning(" Couldn't determine character encoding for filename - using unencoded!\n")
return decodedName
def insertGroup(filename):
grpName = os.path.splitext(os.path.basename(filename))[0]
#There's a problem with adding groups starting with numbers!
root = createGroup('_%s' %(grpName))
root.Label = grpName
return root
def read(filename):
setThumbnail(None)
name, ext = os.path.splitext(filename)
ext = ext.lower()
if (ext == '.ipt'):
ole = Import_IPT.checkVersion(filename)
if (ole):
if (Import_IPT.read(ole)):
return Import_IPT
elif (ext == '.sat'):
if (importerSAT.readText(filename)):
return importerSAT
elif (ext in ['.sab', '.smb', '.smbh']):
if (importerSAT.readBinary(filename)):
return importerSAT
elif (ext == '.dxf'):
if (importerDXF.read(filename)):
return importerDXF
elif (ext == '.iam'):
ole = Import_IPT.checkVersion(filename)
if (ole):
if (Import_IPT.read(ole)):
return Import_IPT
elif (ext == '.ipn'):
ole = Import_IPT.checkVersion(filename)
if (ole):
if (Import_IPT.read(ole)):
return Import_IPT
elif (ext == '.idw'):
ole = Import_IPT.checkVersion(filename)
if (ole):
if (Import_IPT.read(ole)):
return Import_IPT
elif (ext == '.f3d'):
if (importerF3D.read(filename)):
return importerF3D
return None
def isFileValid(filename):
if (not os.path.exists(os.path.abspath(filename))):
logError(u"File doesn't exists (%s)!", os.path.abspath(filename))
return False
if (not os.path.isfile(filename)):
logError(u"Can't import folders!")
return False
if (filename.split(".")[-1].lower() in ("ipt", "iam", "ipn", "idw")):
if (not isOleFile(filename)):
logError(u"ERROR> '%s' is not a valid Autodesk Inventor file!", filename)
return False
return canImport()
def releaseMemory():
setThumbnail(None)
Acis.releaseMemory()
importerClasses.releaseModel()
def adjustView(doc):
if (FreeCAD.GuiUp):
# adjust camara position and orientation
g = FreeCADGui.getDocument(doc.Name)
v = g.ActiveView
c = v.getCameraNode()
p = coin.SbVec3f(1, 1, 1)
o = coin.SbVec3f(0, 0, 0)
u = coin.SbVec3f(0, 1, 0)
c.position.setValue(p)
c.pointAt( o, u )
FreeCADGui.SendMsgToActiveView("ViewFit")
def insert(filename, docname, skip = [], only = [], root = None):
'''
opens an Autodesk Inventor file in the current document
'''
doc = FreeCAD.listDocuments().get(docname)
if (doc):
if (isFileValid(filename)):
logAlways(u"Importing: %s", filename)
reader = read(filename)
if (reader is not None):
name = os.path.splitext(os.path.basename(filename))[0]
name = decode(name)
group = insertGroup(name)
reader.create3dModel(group, doc)
releaseMemory()
FreeCADGui.SendMsgToActiveView("ViewFit")
else:
_open(filename, skip, only, root)
logInfo(u"DONE!")
return
def _open(filename, skip = [], only = [], root = None):
reader = read(filename)
if (reader is not None):
name = os.path.splitext(os.path.basename(filename))[0]
doc = FreeCAD.newDocument(decode(name))
doc.Label = name
doc.CreatedBy = getAuthor()
doc.LastModifiedBy = getLastModifiedBy()
doc.Comment = getComment()
reader.create3dModel(root , doc)
adjustView(doc)
releaseMemory()
return
def open(filename, skip = [], only = [], root = None):
'''
opens an Autodesk Inventor file in a new document
In addition to insert (import), the iProperties are as well added to the document.
'''
if (isFileValid(filename)):
logAlways(u"Reading: %s", os.path.abspath(filename))
_open(filename, skip, only, root)
logInfo(u"DONE!")
return