-
Notifications
You must be signed in to change notification settings - Fork 0
/
subassembly.cpp
485 lines (411 loc) · 15 KB
/
subassembly.cpp
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/*
* subassembly.cpp
*
* Implementation of Subassembly class
* Copyright (C) 2002 lignum Computing, Inc. <lignumcad@lignumcomputing.com>
* $Id$
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <qdom.h>
#include <qregexp.h>
#include <BRep_Builder.hxx>
#include <BRepTools.hxx>
#include <TopoDS.hxx>
#include <TopoDS_Compound.hxx>
#include <TopoDS_Face.hxx>
#include <TopExp_Explorer.hxx>
#include "constants.h"
#include "dburl.h"
#include "model.h"
#include "assembly.h"
#include "part.h"
#include "ocsolid.h"
#include "subassembly.h"
#include "lcdebug.h"
using namespace Space3D;
uint Subassembly::unique_index_ = 0;
QString Subassembly::newName ( void )
{
return tr( "Subassembly[%1]" ).arg( ++unique_index_ );
}
Subassembly::Subassembly ( uint id, const QString& name, Space3D::Page* subassembly,
Space3D::Page* parent )
: Space3D::Figure( id, name, lC::STR::SUBASSEMBLY, parent ),
subassembly_( subassembly ), constraints_( this )
{
init();
}
Subassembly::Subassembly ( uint id, const QDomElement& xml_rep,
Space3D::Page* parent )
: Space3D::Figure( id, QString::null, lC::STR::SUBASSEMBLY, parent ),
constraints_( this )
{
setName( xml_rep.attribute( lC::STR::NAME ) );
QRegExp regexp( tr("Subassembly\\[([0-9]+)\\]" ) );
int position = regexp.indexIn( name() );
if ( position >= 0 ) {
Subassembly::unique_index_ = qMax( regexp.cap(1).toUInt(),
Subassembly::unique_index_ );
}
DBURL url = xml_rep.attribute( lC::STR::SUBASSEMBLY );
subassembly_ = dynamic_cast<Space3D::Page*>( parent->model()->lookup( url ) );
if ( subassembly_ == 0 ) {
parent->model()->addDelayedResolution( this, url.toString() );
delayed_constraints_ = xml_rep;
return;
}
init();
QDomNode n = xml_rep.firstChild();
while ( !n.isNull() ) {
QDomElement e = n.toElement();
if ( !e.isNull() ) {
if ( e.tagName() == lC::STR::CONSTRAINTS ) {
constraints_.addConstraints( e );
}
}
n = n.nextSibling();
}
}
void Subassembly::init ( void )
{
if ( dynamic_cast<Part*>( subassembly_ ) != 0 ) {
Part* part = dynamic_cast<Part*>( subassembly_ );
connect( part->solid(), SIGNAL( modified() ), SLOT( updateSolid() ) );
connect( part->solid(), SIGNAL( materialChanged() ), SIGNAL(materialChanged()));
}
else if ( dynamic_cast<Assembly*>( subassembly_ ) != 0 ) {
Assembly* assembly = dynamic_cast<Assembly*>( subassembly_ );
connect( assembly, SIGNAL( assemblyChanged() ), SLOT( updateSolid() ) );
}
}
void Subassembly::resolved ( const QString& /*db_path*/, ModelItem* item )
{
subassembly_ = dynamic_cast<Space3D::Page*>( item );
init();
QDomNode n = delayed_constraints_.firstChild();
while ( !n.isNull() ) {
QDomElement e = n.toElement();
if ( !e.isNull() ) {
if ( e.tagName() == lC::STR::CONSTRAINTS ) {
constraints_.addConstraints( e );
}
}
n = n.nextSibling();
}
}
void Subassembly::newConstraint ( const AssemblyConstraint* constraint )
{
emit constraintCreated( constraint );
}
void Subassembly::changeConstraint ( const AssemblyConstraint* old_constraint,
const AssemblyConstraint* new_constraint )
{
emit constraintChanged( old_constraint, new_constraint );
}
void Subassembly::changeConstraintOffset ( const AssemblyConstraint* constraint )
{
emit constraintOffsetChanged( constraint );
}
void Subassembly::cancelLast ( void )
{
emit constraintCanceled();
}
void Subassembly::addDependency ( const QVector<uint>& surface_id )
{
#if 0
// Basically, the idea here is to connect to each component of the surface_id
// path whose alteration could affect this subassembly. There are two classes
// of changes which are important:
// 1. Modification of a solid (or, more generally, modification of a part
// but the only interesting thing in a part currently is the solid).
// 2. Transformation of a subassembly to a new location.
//
// So, exhustively examine each element of the path and see what it means to
// us. Note, since part faces are not real ModelItems, we skip the last
// element of the id_path.
QVector<uint> id_path;
id_path.reserve( surface_id.size()-1 );
for ( uint i = 0; i < surface_id.size()-1; ++i ) {
id_path.push_back( surface_id[i] );
ModelItem* item = model()->lookup( id_path );
// The first order check is that we don't already depend on this item.
if ( dependencies_.findRef( item ) != -1 ) {
continue;
}
// If it's a subassembly, then our location may depend on its location
// (unless it is ourself)
else if ( dynamic_cast<Subassembly*>( item ) != 0 && item != this ) {
dependencies_.append( item );
connect( item, SIGNAL( locationChanged() ), SLOT( updateLocation() ) );
}
// If it's a solid, then we probably depend on the exact geometry
// of its faces.
else if ( dynamic_cast<Space3D::OCSolid*>( item ) != 0 ) {
// Unless our own subassembly model *is* this part.
if ( dynamic_cast<Part*>( subassembly_ ) != 0 &&
dynamic_cast<Part*>( subassembly_ )->solid() == item )
continue;
dependencies_.append( item );
connect( dynamic_cast<Space3D::OCSolid*>(item), SIGNAL( modified() ),
SLOT( updateLocation() ) );
}
}
#else
// I think the above was too much. We should depend on a signal from the
// top level subassembly to indicate that one of its children has been
// modified. At the subassembly level, it doesn't matter if the reference
// has changed due a solid resize or just a sub-subassembly having been
// moved. I think?...
QVector<uint> id_path;
id_path.reserve( surface_id.size()-1 );
for ( int i = 0; i < surface_id.size()-1; ++i ) {
id_path.push_back( surface_id[i] );
ModelItem* item = model()->lookup( id_path );
// The first order check is that we don't already depend on this item.
QListIterator<std::shared_ptr<ModelItem>> it ( dependencies_);
bool found = false;
while(it.hasNext()) {
if(it.next().get() == item) {
found = true;
break;
}
}
if ( found ) {
continue;
}
// If it's the top level subassembly of the reference, then our location
// may depend on its location (unless it is ourself)
else if ( dynamic_cast<Subassembly*>( item ) != 0 && item != this ) {
std::shared_ptr<ModelItem> modItem = std::shared_ptr<ModelItem>(item);
dependencies_.append( modItem );
connect( item, SIGNAL( locationChanged() ), SLOT( updateLocation() ) );
break; // And that's it. Don't look any more.
}
#if 0
// If it's a solid, then we probably depend on the exact geometry
// of its faces.
else if ( dynamic_cast<Space3D::OCSolid*>( item ) != 0 ) {
// Unless our own subassembly model *is* this part.
if ( dynamic_cast<Part*>( subassembly_ ) != 0 &&
dynamic_cast<Part*>( subassembly_ )->solid() == item )
continue;
dependencies_.append( item );
connect( dynamic_cast<Space3D::OCSolid*>(item), SIGNAL( modified() ),
SLOT( updateLocation() ) );
}
#endif
}
#endif
}
void Subassembly::rotate ( gp_Dir direction, double angle )
{
// cout << path() << " rotate" << endl;
// Rotate the subassembly about its current origin in the global
// coordinate system. This is equivalent to translating the solid
// to the origin performing the rotation and translating back
// to the original displacement.
gp_Trsf transform = location_;
gp_Trsf rotation;
rotation.SetRotation( gp_Ax1( transform.TranslationPart(), direction ), angle );
transform.PreMultiply( rotation );
location_ = TopLoc_Location( transform );
#if 0
cout << path() << " rotated [before emit]" << endl;
emit locationChanged();
cout << path() << " rotated [after emit]" << endl;
#endif
}
void Subassembly::translate ( gp_Vec displacement )
{
// cout << path() << " translate" << endl;
gp_Trsf transform = location_;
gp_Trsf translation;
translation.SetTranslation( displacement );
transform.PreMultiply( translation );
location_ = TopLoc_Location( transform );
#if 0
cout << path() << " translated [before emit]" << endl;
emit locationChanged();
cout << path() << " translated [after emit]" << endl;
#endif
}
void Subassembly::updateSolid ( void )
{
constraints_.recompute();
// cout << now() << path() << " changed due to solid change [before emit]" << endl;
emit locationChanged();
// cout << now() << path() << " changed due to solid change [after emit]" << endl;
}
void Subassembly::updateLocation ( void )
{
constraints_.recompute();
// Not ordinarily necessary, since the OpenGL transform matrices are
// computed on the fly; but may be necessary for hidden line mode...
// cout << now() << path() << " changed due to location change [before emit]" << endl;
emit locationChanged();
// cout << now() << path() << " changed due to location change [after emit]" << endl;
}
void Subassembly::updateMaterial ( void )
{
emit materialChanged();
}
void Subassembly::write ( QDomElement& xml_rep ) const
{
QDomDocument document = xml_rep.ownerDocument();
QDomElement subassembly_element = document.createElement( lC::STR::SUBASSEMBLY );
subassembly_element.setAttribute( lC::STR::ID, id() );
subassembly_element.setAttribute( lC::STR::NAME, name() );
subassembly_element.setAttribute( lC::STR::SUBASSEMBLY,
subassembly_->dbURL().toString() );
constraints_.write( subassembly_element );
xml_rep.appendChild( subassembly_element );
}
void Subassembly::copyWrite ( QDomElement& /*xml_rep*/ ) const {}
ModelItem* Subassembly::lookup ( QStringList& /*path_components*/ ) const { return 0; }
// Find the ModelItem given by the path.
ModelItem* Subassembly::lookup ( QVector<uint>& id_path ) const
{
if ( subassembly_->id() == id_path[0] ) {
id_path.erase( id_path.begin() );
if ( id_path.empty() )
return subassembly_;
else
return subassembly_->lookup( id_path );
}
return 0; // Really an error...
}
// Find the OpenCASCADE type associated with the path (presumably some kind
// of geometry)
Handle(Standard_Type) Subassembly::lookupType ( QStringList& path_components ) const
{
// The front path component is the name of a figure with ".type" appended
// to it.
int dot_pos = path_components.front().lastIndexOf( '.' );
QString name = path_components.front().left( dot_pos );
QString type = path_components.front().right( path_components.front().length()
- dot_pos - 1 );
if ( subassembly_->name() == name && subassembly_->type() == type ) {
path_components.erase( path_components.begin() );
return subassembly_->lookupType( path_components );
}
return Handle(Standard_Type)();
}
// Find the OpenCASCADE type associated with the path (presumably some kind
// of geometry)
Handle(Standard_Type) Subassembly::lookupType ( QVector<uint>& id_path ) const
{
if ( subassembly_->id() == id_path[0] ) {
id_path.erase( id_path.begin() );
return subassembly_->lookupType( id_path );
}
return Handle(Standard_Type)();
}
// Find the OpenCASCADE shape associated with the path (presumably some kind
// of geometry)
TopoDS_Shape Subassembly::lookupShape ( QStringList& path_components ) const
{
// The front path component is the name of a figure with ".type" appended
// to it.
int dot_pos = path_components.front().lastIndexOf( '.' );
QString name = path_components.front().left( dot_pos );
QString type = path_components.front().right( path_components.front().length()
- dot_pos - 1 );
TopoDS_Shape shape;
if ( subassembly_->name() == name && subassembly_->type() == type ) {
path_components.erase( path_components.begin() );
// The shape returned here must be modified by the transformation
// of the subassembly.
shape = subassembly_->lookupShape( path_components );
shape.Location( location_ * shape.Location() );
}
return shape;
}
// Find the OpenCASCADE shape associated with the path (presumably some kind
// of geometry)
TopoDS_Shape Subassembly::lookupShape ( QVector<uint>& id_path ) const
{
TopoDS_Shape shape;
if ( subassembly_->id() == id_path[0] ) {
id_path.erase( id_path.begin() );
shape = subassembly_->lookupShape( id_path );
shape.Location( location_ * shape.Location() );
}
return shape;
}
bool Subassembly::referenced ( const Subassembly* target ) const
{
// What to do? Look at the ID of each constraint's reference1 and see
// if target is a prefix match of the reference?
QVector<uint> target_id = target->ID();
QListIterator< std::shared_ptr<AssemblyConstraint>> constraint = constraints_.constraints();
while ( constraint.hasNext() ) {
QVector<uint> reference1 = constraint.next()->reference1();
if ( !reference1.empty() && reference1.size() >= target_id.size() ) {
bool equal = true;
for ( int i = 0; i < target_id.size(); i++ ) {
if ( reference1[i] != target_id[i] ) {
equal = false;
break;
}
}
if ( equal )
return true;
}
}
return false;
}
QString Subassembly::idPath ( QVector<uint> id_path ) const
{
if ( subassembly_->id() != id_path[0] )
return QString::null; // Really an error...
id_path.erase( id_path.begin() );
if ( id_path.empty() )
return subassembly_->name() + '.' + subassembly_->type();
return subassembly_->name() + '.' + subassembly_->type() + '/' +
subassembly_->idPath( id_path );
}
void Subassembly::pathID ( QStringList& path_components,
QVector<uint>& id_path ) const
{
// The front path component is the name of a figure with ".type" appended
// to it.
int dot_pos = path_components.front().lastIndexOf( '.' );
QString name = path_components.front().left( dot_pos );
QString type = path_components.front().right( path_components.front().length()
- dot_pos - 1 );
if ( subassembly_->name() == name && subassembly_->type() == type ) {
id_path.push_back( subassembly_->id() );
path_components.erase( path_components.begin() );
if ( !path_components.empty() ) {
subassembly_->pathID( path_components, id_path );
}
}
}
void Subassembly::compound ( TopoDS_Shape& assembly )
{
BRep_Builder builder;
if ( dynamic_cast<Part*>( subassembly_ ) != 0 ) {
TopoDS_Shape placed_solid =
dynamic_cast<Part*>( subassembly_ )->shape().Located( location_ );
builder.Add( assembly, placed_solid );
}
else if ( dynamic_cast<Assembly*>( subassembly_ ) != 0 ) {
TopoDS_Shape placed_solid =
dynamic_cast<Assembly*>( subassembly_ )->compound().Located( location_ );
builder.Add( assembly, placed_solid );
}
}