forked from saggita/rhinoviewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RhModel.mm
710 lines (597 loc) · 24 KB
/
RhModel.mm
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
/* $NoKeywords: $ */
/*
//
// Copyright (c) 1993-2011 Robert McNeel & Associates. All rights reserved.
// Rhinoceros is a registered trademark of Robert McNeel & Assoicates.
//
// THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
// ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR PURPOSE AND OF
// MERCHANTABILITY ARE HEREBY DISCLAIMED.
//
// For complete openNURBS copyright information see <http://www.opennurbs.org>.
//
////////////////////////////////////////////////////////////////
*/
#import "RhModel.h"
#import "DisplayMesh.h"
@interface RhModel ()
- (void) meshPreparationProgress: (NSNumber*) progress;
- (void) meshPreparationDidSucceed;
- (void) meshPreparationDidFailWithError: (NSError*) error;
@end
@implementation RhModel
@synthesize title, description, source, urlString, cachesDirectoryName, documentsFilename, bundleName, isSample;
@synthesize fileSize, meshObjectCount, renderMeshCount, geometryCount, brepCount, brepWithMeshCount, downloaded;
@synthesize preparationCancelled, readingModel, continueReading, continueReadingLock, readSuccessfully, initializationFailed, meshes, transmeshes;
@synthesize pickBitmap;
// Helper method for creating a full path from a file name that is in the ~/Library/Caches directory
- (NSString*) cachesPathFromDirectory: (NSString*) directory filename: (NSString*) filename
{
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES);
NSString* filePath = [paths objectAtIndex:0];
if (directory.length > 0)
filePath = [filePath stringByAppendingPathComponent: directory];
if (filename.length > 0)
filePath = [filePath stringByAppendingPathComponent: filename];
return filePath;
}
// Helper method for creating a full path for one of our files that is in our ~/Library/Caches subdirectory
- (NSString*) cachesPathForName: (NSString*) fileOrDirectoryName
{
if (cachesDirectoryName == nil) {
self.cachesDirectoryName = RhinoApp.newUUID;
NSString* cachesDirectoryPath = [self cachesPathFromDirectory: cachesDirectoryName filename: nil];
BOOL rc = [[NSFileManager defaultManager] createDirectoryAtPath: cachesDirectoryPath
withIntermediateDirectories: YES
attributes: nil
error: nil];
}
return [self cachesPathFromDirectory: cachesDirectoryName filename: fileOrDirectoryName];
}
#pragma mark Initialization
- (id) init
{
self = [super init];
if (self) {
continueReadingLock = [[NSLock alloc] init];
}
return self;
}
- (id) initWithDictionary: (NSDictionary*) dictionary
{
self = [self init];
if (self) {
NSString* str;
str = [dictionary objectForKey: @"IRModelTitle"];
title = [[[NSBundle mainBundle] localizedStringForKey: str value: str table: nil] copy];
str = [dictionary objectForKey: @"IRModelDescription"];
description = [[[NSBundle mainBundle] localizedStringForKey: str value: str table: nil] copy];
urlString = [[dictionary objectForKey: @"IRModelURL"] copy];
modelID = [[dictionary objectForKey: @"IRModelID"] copy];
cachesDirectoryName = [[dictionary objectForKey: @"IRCachesDirectory"] copy];
documentsFilename = [[dictionary objectForKey: @"IRDocumentsFilename"] copy];
previewFilename = [[dictionary objectForKey: @"IRPreviewFilename"] copy];
bundleName = [[dictionary objectForKey: @"IRBundleName"] copy];
isSample = [[dictionary objectForKey: @"IRIsSample"] boolValue];
downloaded = [[dictionary objectForKey: @"IRDownloaded"] boolValue];
if (bundleName != nil)
downloaded = YES;
source = [[dictionary objectForKey: @"IRModelSource"] intValue];
}
return self;
}
- (void) dealloc
{
[continueReadingLock release];
delete onMacModel;
[meshes release];
[transmeshes release];
[pickBitmap release];
[title release];
[description release];
[urlString release];
[modelID release];
[thumbnailImage release];
[previewImage release];
[cachesDirectoryName release];
[documentsFilename release];
[previewFilename release];
[thumbnailFilename release];
[bundleName release];
[super dealloc];
}
// This model has just been created by a reset. Set up our disk data
- (void) initializeSampleModel
{
if (bundleName) {
self.documentsFilename = [[NSBundle mainBundle] pathForResource: bundleName ofType:@"3dm"];
self.downloaded = YES;
}
}
#pragma mark NSCoding
- (void)encodeWithCoder:(NSCoder *) coder
{
// [super encodeWithCoder: coder];
[coder encodeObject: title forKey: @"IRModelTitle"];
[coder encodeObject: description forKey: @"IRModelDescription"];
[coder encodeObject: urlString forKey: @"IRModelURL"];
[coder encodeObject: modelID forKey: @"IRModelID"];
[coder encodeObject: cachesDirectoryName forKey: @"IRCachesDirectory"];
[coder encodeObject: documentsFilename forKey: @"IRDocumentsFilename"];
[coder encodeObject: previewFilename forKey: @"IRPreviewFilename"];
[coder encodeObject: thumbnailFilename forKey: @"IRThumbnailFilename"];
[coder encodeObject: bundleName forKey: @"IRBundleName"];
[coder encodeObject: [NSNumber numberWithBool: isSample] forKey: @"IRIsSample"];
[coder encodeObject: [NSNumber numberWithBool: downloaded] forKey: @"IRDownloaded"];
[coder encodeObject: [NSNumber numberWithInt: source] forKey: @"IRModelSource"];
if (thumbnailImage) {
if (thumbnailFilename == nil) {
NSData* pngData = UIImagePNGRepresentation (thumbnailImage);
[coder encodeObject: pngData forKey: @"IRModelThumbnailImage"];
}
if ([thumbnailImage respondsToSelector: @selector(scale)]) {
CGFloat thumbnailScale = [thumbnailImage scale];
[coder encodeObject: [NSNumber numberWithFloat: thumbnailScale] forKey: @"IRThumbnailScale"];
}
}
}
- (id)initWithCoder: (NSCoder *) decoder
{
self = [self init];
if (self) {
title = [[decoder decodeObjectForKey: @"IRModelTitle"] copy];
description = [[decoder decodeObjectForKey: @"IRModelDescription"] copy];
urlString = [[decoder decodeObjectForKey: @"IRModelURL"] copy];
modelID = [[decoder decodeObjectForKey: @"IRModelID"] copy];
cachesDirectoryName = [[decoder decodeObjectForKey: @"IRCachesDirectory"] copy];
documentsFilename = [[decoder decodeObjectForKey: @"IRDocumentsFilename"] copy];
previewFilename = [[decoder decodeObjectForKey: @"IRPreviewFilename"] copy];
thumbnailFilename = [[decoder decodeObjectForKey: @"IRThumbnailFilename"] copy];
bundleName = [[decoder decodeObjectForKey: @"IRBundleName"] copy];
isSample = [[decoder decodeObjectForKey: @"IRIsSample"] boolValue];
downloaded = [[decoder decodeObjectForKey: @"IRDownloaded"] boolValue];
source = [[decoder decodeObjectForKey: @"IRModelSource"] intValue];
NSData* pngData = [decoder decodeObjectForKey: @"IRModelThumbnailImage"];
if (pngData)
thumbnailImage = [[UIImage imageWithData: pngData] retain];
if (thumbnailFilename != nil) {
NSString* thumbnailPath = [self cachesPathForName: thumbnailFilename];
if ([[NSFileManager defaultManager] fileExistsAtPath: thumbnailPath]) {
[thumbnailImage release];
thumbnailImage = [[UIImage imageWithContentsOfFile: thumbnailPath] retain];
}
}
if ([thumbnailImage respondsToSelector: @selector(scale)]) {
// fix up thumbnail scale
CGFloat thumbnailScale = [[decoder decodeObjectForKey: @"IRThumbnailScale"] floatValue];
if (thumbnailScale != 0.0 && thumbnailScale != thumbnailImage.scale) {
[thumbnailImage release];
thumbnailImage = [[UIImage imageWithCGImage: thumbnailImage.CGImage scale: [RhinoApp screenScale] orientation: UIImageOrientationUp] retain];
}
}
}
return self;
}
#pragma mark Utilities
- (EX_ONX_Model*) onMacModel
{
return onMacModel;
}
- (ON_BoundingBox) boundingBox
{
if (onMacModel)
return onMacModel->BoundingBox();
return ON_BoundingBox::EmptyBoundingBox;
}
- (NSString*) debugDescription
{
return [NSString stringWithFormat: @"RhModel %p %@ meshes:%@", self, title, [meshes description]];
}
// return full pathname of 3dm file
- (NSString*) modelPath
{
if (documentsFilename) {
return documentsFilename;
}
return nil;
}
- (NSString*) basename
{
return title;
}
- (BOOL) hasDocumentsName: (NSString*) aName
{
if (aName != nil && documentsFilename != nil)
return [documentsFilename localizedCaseInsensitiveCompare: aName] == NSOrderedSame;
return NO;
}
- (BOOL) needsPassword
{
return NO;
}
- (BOOL) meshesInitialized
{
return (meshes != nil || transmeshes != nil) || !initializationFailed;
}
// delete all cached data but not the model
- (void) deleteCaches
{
if (cachesDirectoryName)
[[NSFileManager defaultManager] removeItemAtPath: [self cachesPathForName: nil] error: nil];
self.cachesDirectoryName = nil;
}
// delete everything, including our containing directory
- (void) deleteAll
{
[self deleteCaches];
downloaded = NO;
}
- (void) cleanUp
{
delete onMacModel;
onMacModel = nil;
[meshes release];
meshes = nil;
[transmeshes release];
transmeshes = nil;
[pickBitmap release];
pickBitmap = nil;
}
// revert to undownloaded status
- (void) undownload
{
[self deleteAll];
[self cleanUp];
}
- (void)alertView: (UIAlertView*) alertView willDismissWithButtonIndex:(NSInteger) buttonIndex
{
if (buttonIndex == 0)
self.continueReading = 1; // 1 = continue
else
self.continueReading = 0; // 0 = stop
// unblock the reading thread
[continueReadingLock unlock];
}
#pragma mark Accessors
- (long) polygonCount
{
long triangles = 0;
for (DisplayMesh* me in meshes)
triangles += [me triangleCount];
for (DisplayMesh* me in transmeshes)
triangles += [me triangleCount];
return triangles;
}
- (NSString*) modelID
{
return [modelID copy];
}
- (void) setModelID: (NSString*) newValue
{
if (![modelID isEqualToString: newValue]) {
// If the modelID has changed, the user likely modified a file in the Documents folder.
// Invalidate any cached data.
[self deleteCaches];
[modelID release];
modelID = [newValue copy];
}
}
#pragma mark Mesh Caches
//
// Models with large meshes must partition the meshes before displaying them on the iPhone.
// Partitioning meshes is a lengthy process and can take > 80% of the model loading time.
// We save the raw VBO data of a mesh in an archive after a mesh has been partitioned
// and use that archive to create the VBOs next time we display the model.
//
- (BOOL) loadMeshCaches: (ON_Mesh*) mesh withAttributes: (ON_3dmObjectAttributes&) attr withMaterial: (ON_Material&) material
{
NSString* meshUUIDStr = uuid2ns(attr.m_uuid);
NSString* meshCachePath = [self cachesPathForName: [meshUUIDStr stringByAppendingPathExtension: @"meshes"]];
NSArray* displayMeshes = [NSKeyedUnarchiver unarchiveObjectWithFile: meshCachePath];
if (displayMeshes == nil)
return NO;
for (DisplayMesh* me in displayMeshes)
[me restoreUsingMesh: mesh material: material];
if ( material.Transparency() == 0 )
[meshes addObjectsFromArray: displayMeshes];
else
[transmeshes addObjectsFromArray: displayMeshes];
return YES;
}
- (void) saveDisplayMeshes: (NSArray*) cachedMeshes forMesh: (ON_Mesh*) mesh withAttributes: (ON_3dmObjectAttributes&) attr withMaterial: (ON_Material&) material
{
NSString* meshUUIDStr = uuid2ns(attr.m_uuid);
NSString* meshCachePath = [self cachesPathForName: [meshUUIDStr stringByAppendingPathExtension: @"meshes"]];
[NSKeyedArchiver archiveRootObject: cachedMeshes toFile: meshCachePath];
}
#pragma mark Meshes
-(NSError*) meshError: (NSString*) errorStr
{
NSDictionary* userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
NSLocalizedString(@"Cannot initialize model", @"title for warning dialog"), NSLocalizedDescriptionKey,
errorStr, NSLocalizedFailureReasonErrorKey,
nil];
return [NSError errorWithDomain: @"com.yourcompany.rhinoviewer" code: 33 userInfo: userInfo];
}
- (void) createDisplayMeshes: (ON_Mesh*) mesh withAttributes: (ON_3dmObjectAttributes&) attr withMaterial: (ON_Material&) material
{
// will we create more than one partition?
int vertex_count = mesh->VertexCount();
const int triangle_count = mesh->TriangleCount() + 2*mesh->QuadCount();
BOOL multipleMeshPartitions = vertex_count > USHRT_MAX-3 || triangle_count > INT_MAX-3;
if (multipleMeshPartitions && [self loadMeshCaches: mesh withAttributes: attr withMaterial: material])
return; // successfully created DisplayMesh objects from the cache. We are done.
const ON_MeshPartition* partition = mesh->CreatePartition(USHRT_MAX-3, INT_MAX-3);
if (partition == NULL)
return; // invalid mesh, ignore
int partCount = partition->m_part.Count();
NSMutableArray* displayMeshes = [[NSMutableArray alloc] initWithCapacity: partCount];
// DLog (@" ");
// DLog (@"mesh %p VertexCount %d, triangleCount %d vertexIndexCount %d FaceCount %d", mesh, mesh->VertexCount(), mesh->TriangleCount(), mesh->TriangleCount()*3 + mesh->QuadCount()*6, mesh->FaceCount());
// DLog (@"partition has %d parts", partition->m_part.Count());
for (int idx=0; idx<partCount; idx++) {
const struct ON_MeshPart& part = partition->m_part[idx];
// DLog (@"m_V[%d] -> m_V[%d], m_F[%d] -> m_F[%d] vertexCount %d, triangleCount %d", part.vi[0], part.vi[1], part.fi[0], part.fi[1], part.vertex_count, part.triangle_count);
DisplayMesh* me = [[DisplayMesh alloc] initWithMesh: mesh index: idx material: material saveVBOData: multipleMeshPartitions];
if (me) {
if ( [me isOpaque] )
[meshes addObject: me];
else
[transmeshes addObject: me];
[displayMeshes addObject: me];
[me release];
}
}
if (multipleMeshPartitions) {
[self saveDisplayMeshes: displayMeshes forMesh: mesh withAttributes: attr withMaterial: material];
for (DisplayMesh* me in displayMeshes)
[me deleteVBOData];
}
[displayMeshes release];
}
- (void) addAnyMesh: (const ON_Mesh*) mesh withAttributes: (ON_3dmObjectAttributes&) attr
{
[self meshPreparationProgress: [NSNumber numberWithFloat: -1.0]];
ON_Material material;
onMacModel->GetRenderMaterial ( attr, material );
// If our render material is the default material, modify our material to match the Rhino default material
if (material.MaterialIndex() < 0)
material.SetDiffuse( ON_Color( 255, 255, 255));
[self createDisplayMeshes: const_cast<ON_Mesh*> (mesh) withAttributes: attr withMaterial: material]; // cast away const
}
- (void) addRenderMesh: (const ON_Mesh*) mesh withAttributes: (ON_3dmObjectAttributes&) attr
{
renderMeshCount++;
[self addAnyMesh: mesh withAttributes: attr];
}
- (void) addMeshObject: (const ON_Mesh*) mesh withAttributes: (ON_3dmObjectAttributes&) attr
{
meshObjectCount++;
[self addAnyMesh: mesh withAttributes: attr];
}
// This potentially long-running process is run in a separate thread
- (void) prepareMeshes
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
self.readingModel = YES;
[self.continueReadingLock lock];
self.continueReading = 1;
[self.continueReadingLock unlock];
NSError* prepareMeshesError = nil;
if ([self isDownloaded]) {
// Use a helper class to read the 3DM file
if (onMacModel == nil) {
self.meshes = [NSMutableArray array];
self.transmeshes = [NSMutableArray array];
renderMeshCount = 0;
meshObjectCount = 0;
brepCount = 0;
brepWithMeshCount = 0;
// get the file size
NSDictionary * attributes = [[NSFileManager defaultManager] attributesOfItemAtPath: [self modelPath] error:nil];
NSNumber *theFileSize;
if (theFileSize = [attributes objectForKey: NSFileSize])
fileSize = [theFileSize intValue];
// show we have started reading the meshes
[self meshPreparationProgress: [NSNumber numberWithFloat: -1.0]];
// The initWithFilename call will read the OpenNURBS file. As each object is read,
// the EX_ONX_Model::ShouldKeepObject() function in this source file is called to
// inspect and perform any operations on the object.
onMacModel = new EX_ONX_Model;
ON_BOOL32 rc = onMacModel->initWithFilename ([[self modelPath] UTF8String]);
if (rc) {
// look for models that cannot be displayed
if ((meshes.count == 0) && (transmeshes.count == 0)) {
if (brepCount > 0 && brepWithMeshCount == 0)
prepareMeshesError = [self meshError: NSLocalizedString(@"This model is only wireframes and cannot be displayed. Save the model in shaded mode and download again.",@"error message when reading 3DM file")];
else if (geometryCount > 0 && brepWithMeshCount == 0)
prepareMeshesError = [self meshError: NSLocalizedString(@"This model has no renderable geometry.",@"error message when reading 3DM file")];
else
prepareMeshesError = [self meshError: NSLocalizedString(@"This model is empty.", @"error message when reading 3DM file")];
rc = 0;
}
}
if (!rc) {
self.meshes = nil;
self.transmeshes = nil;
delete onMacModel;
onMacModel = nil;
if (preparationCancelled)
prepareMeshesError = [self meshError: NSLocalizedString(@"Initialization cancelled.", @"error message when reading 3DM file")];
else if (prepareMeshesError == nil)
prepareMeshesError = [self meshError: NSLocalizedString(@"This model is corrupt and cannot be displayed.", @"error message when reading 3DM file")];
}
self.readingModel = NO;
self.readSuccessfully = rc;
}
}
if (prepareMeshesError)
[self meshPreparationDidFailWithError: prepareMeshesError];
else
[self meshPreparationDidSucceed];
[pool release];
}
- (void) cancelMeshInitialization
{
preparationCancelled = YES;
}
- (void) meshPreparationProgress: (NSNumber*) progress
{
// pass the message to the preparationDelegate
if ([preparationDelegate respondsToSelector: @selector(meshPreparationProgress:)])
[preparationDelegate performSelectorOnMainThread: @selector(meshPreparationProgress:) withObject: progress waitUntilDone: NO];
}
- (void) meshPreparationDidSucceed
{
if ([preparationDelegate respondsToSelector: @selector(preparationDidSucceed)])
[preparationDelegate performSelectorOnMainThread: @selector(preparationDidSucceed) withObject: nil waitUntilDone: NO];
preparationDelegate = nil;
}
- (void) meshPreparationDidFailWithError: (NSError*) error
{
if ([preparationDelegate respondsToSelector: @selector(preparationDidFailWithError:)])
[preparationDelegate performSelectorOnMainThread: @selector(preparationDidFailWithError:) withObject: error waitUntilDone: NO];
preparationDelegate = nil;
initializationFailed = YES;
}
#pragma mark Prepare Model
- (void) prepareModelWithDelegate: (id) delegate
{
preparationCancelled = NO;
preparationDelegate = delegate;
self.downloaded = YES;
// Start mesh initialization in a second thread. We need the UI to stay alive so we can
// cancel the mesh preparation and so we can receive low memory warnings.
[NSThread detachNewThreadSelector: @selector(prepareMeshes) toTarget: self withObject: nil];
}
- (void) cancelModelPreparation
{
// The preparation commands will check this variable and return a NSError
preparationCancelled = YES;
}
- (void) cancelModelPreparationSilently
{
// The preparation commands will check this variable and return a NSError
preparationCancelled = YES;
}
#pragma mark Current model
// we are becoming the current model
- (BOOL) becomeCurrentModel
{
return [self isDownloaded];
}
// We are no longer the current model
- (void) resignCurrentModel
{
[self cancelModelPreparationSilently];
[self performSelector: @selector(cleanUp) withObject: nil afterDelay: 0.1];
}
@end
// This function is called right after reading the 3DM properties. We construct a
// modelID string from the ON_3dmRevisionHistory and ON_3dmApplication objects with
// the hope that this will uniquely identify the file contents. If the user changes
// the file contents, we should be able to detect this because the constructed modelID
// string will also change.
void EX_ONX_Model::InspectProperties (ON_3dmProperties& properties)
{
ON_wString revisionIDString;
ON_TextLog revisionIDLog ( revisionIDString );
properties.m_RevisionHistory.Dump(revisionIDLog);
NSString* revisionID = w2ns(revisionIDString);
ON_wString appIDString;
ON_TextLog appIDLog ( appIDString );
properties.m_Application.Dump(appIDLog);
NSString* appID = w2ns(appIDString);
if (appID == nil)
appID = @"Name: Unknown\n";
RhModel* currentModel = RhinoApp.currentModel;
[currentModel setModelID: [revisionID stringByAppendingString: appID]];
}
//
// We create a DisplayMesh object for each render mesh object we find. If we encounter an ON_Mesh object,
// we create a DisplayMesh object from the ON_Mesh. For any ON_Brep objects, we create Displaymesh objects
// from the render mesh. To conserve memory, we always return 0 which tells the object reading code
// to discard the object it has just read.
//
// This function returns +1 to keep object; 0 to discard object; -1 to stop reading file
//
int EX_ONX_Model::ShouldKeepObject (ON_Object* pObject, ON_3dmObjectAttributes& attr)
{
RhModel* currentModel = RhinoApp.currentModel;
[currentModel.continueReadingLock lock];
int shouldContinue = currentModel.continueReading;
[currentModel.continueReadingLock unlock];
if (shouldContinue == 0) {
DLog (@"saw outOfMemoryWarning, quitting ShouldKeepObject");
return -1;
}
if ([currentModel preparationCancelled])
return -1;
// ensure the object is visible
if (!attr.IsVisible())
return 0;
// ensure the object's layer is visible
int layerIndex = attr.m_layer_index;
if (layerIndex >= 0 && layerIndex < m_layer_table.Count()) {
ON_Layer& layer = m_layer_table[layerIndex];
if (!layer.IsVisible())
return 0;
}
// calculate bounding box as we read objects
const ON_Geometry* geo = ON_Geometry::Cast(pObject);
if ( geo ) {
currentModel.geometryCount++;
m__object_table_bbox.Union(geo->BoundingBox());
}
if (pObject->ObjectType() == ON::mesh_object) {
ON_Mesh* mesh = (ON_Mesh*) pObject;
if ( 0 == mesh->HiddenVertexCount() )
mesh->DestroyHiddenVertexArray();
if ( !mesh->HasVertexNormals() && mesh->m_V.Count() > 0 && mesh->m_F.Count() > 0) {
// 26 September 2003 Dale Lear -
// Some 3dm files have meshes with no normals and this messes up the shading code.
mesh->ComputeVertexNormals();
}
[currentModel addMeshObject: mesh withAttributes: attr];
return 0; // do not keep ON::mesh_object
}
else if (pObject->ObjectType() == ON::brep_object) {
currentModel.brepCount++;
ON_Brep* pBrep = (ON_Brep*) pObject;
ON_SimpleArray< const ON_Mesh* > meshes;
int count = pBrep->GetMesh( ON::render_mesh, meshes );
if (count > 0)
currentModel.brepWithMeshCount++;
if ( count == 1 )
{
if ( meshes[0] && meshes[0]->VertexCount() )
[currentModel addRenderMesh: meshes[0] withAttributes: attr];
}
else
{
ON_Mesh m;
// Sometimes it's possible to have lists of NULL ON_Meshes...Rhino
// can put them there as placeholders for badly formed/meshed breps.
// Therefore, we need to always make sure we're actually looking at
// a mesh that contains something and/or is not NULL.
for (int i = 0; i < count; i++)
{
// If we have a valid pointer, append the mesh to our accumulator mesh...
if ( meshes[i] )
m.Append( *meshes[i] );
}
// See if the end result actually contains anything and add it to
// our model if it does...
if ( m.VertexCount() > 0 )
[currentModel addRenderMesh: &m withAttributes: attr];
}
return 0; // do not keep ON::brep_object
}
else if (pObject->ObjectType() == ON::extrusion_object) {
// extrusion objects do not have a mesh
return 0; // do not keep ON::extrusion_object
}
else {
return 0; // do not keep anything else
}
}