0027313: Exception during WriteStep with PMI
[occt.git] / src / STEPCAFControl / STEPCAFControl_Writer.cxx
CommitLineData
b311480e 1// Created on: 2000-08-15
2// Created by: Andrey BETENEV
973c2be1 3// Copyright (c) 2000-2014 OPEN CASCADE SAS
7fd59977 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
d5f74e42 7// This library is free software; you can redistribute it and/or modify it under
8// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 9// by the Free Software Foundation, with special exception defined in the file
10// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11// distribution for complete text of the license and disclaimer of any warranty.
7fd59977 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
b311480e 15
16// CURRENT LIMITATIONS:
7fd59977 17// when val props and names assigned to instance of
18// component in assembly, it is in fact supposed that only one CDSR corresponds
19// to such shape. This can be wrong and should be handled more carefully
20// (analysis of SDRs which the CDSR links should be done)
7fd59977 21// Names and validation props are supported for top-level shapes only
22
7fd59977 23#include <BRep_Builder.hxx>
18f7c9a5 24#include <GeomToStep_MakeCartesianPoint.hxx>
7fd59977 25#include <HeaderSection_FileSchema.hxx>
42cf5bc1 26#include <Interface_EntityIterator.hxx>
27#include <Interface_Static.hxx>
28#include <Message_Messenger.hxx>
7fd59977 29#include <MoniTool_DataMapIteratorOfDataMapOfShapeTransient.hxx>
b0cef606 30#include <NCollection_Vector.hxx>
42cf5bc1 31#include <OSD_Path.hxx>
7fd59977 32#include <Quantity_TypeOfColor.hxx>
42cf5bc1 33#include <StepAP214_Protocol.hxx>
b0cef606 34#include <StepAP242_DraughtingModelItemAssociation.hxx>
6595eee7 35#include <StepAP242_GeometricItemSpecificUsage.hxx>
42cf5bc1 36#include <StepBasic_DerivedUnit.hxx>
37#include <StepBasic_DerivedUnitElement.hxx>
38#include <StepBasic_HArray1OfDerivedUnitElement.hxx>
39#include <StepBasic_LengthMeasureWithUnit.hxx>
7fd59977 40#include <StepBasic_MeasureValueMember.hxx>
42cf5bc1 41#include <StepBasic_Product.hxx>
42#include <StepBasic_ProductDefinition.hxx>
43#include <StepBasic_ProductDefinitionFormation.hxx>
44#include <StepBasic_ProductDefinitionRelationship.hxx>
7fd59977 45#include <StepBasic_SiUnitAndLengthUnit.hxx>
42cf5bc1 46#include <StepBasic_SiUnitAndMassUnit.hxx>
6595eee7 47#include <StepBasic_SiUnitAndPlaneAngleUnit.hxx>
42cf5bc1 48#include <STEPCAFControl_ActorWrite.hxx>
49#include <STEPCAFControl_Controller.hxx>
50#include <STEPCAFControl_DictionaryOfExternFile.hxx>
51#include <STEPCAFControl_ExternFile.hxx>
52#include <STEPCAFControl_IteratorOfDictionaryOfExternFile.hxx>
53#include <STEPCAFControl_Writer.hxx>
54#include <STEPConstruct.hxx>
55#include <STEPConstruct_DataMapOfAsciiStringTransient.hxx>
56#include <STEPConstruct_DataMapOfPointTransient.hxx>
57#include <STEPConstruct_ExternRefs.hxx>
727b5ad9 58#include <STEPCAFControl_GDTProperty.hxx>
42cf5bc1 59#include <STEPConstruct_Styles.hxx>
60#include <STEPConstruct_ValidationProps.hxx>
61#include <STEPControl_StepModelType.hxx>
62#include <STEPControl_Writer.hxx>
7fd59977 63#include <StepData_Logical.hxx>
42cf5bc1 64#include <StepData_StepModel.hxx>
7fd59977 65#include <StepDimTol_AngularityTolerance.hxx>
66#include <StepDimTol_CircularRunoutTolerance.hxx>
67#include <StepDimTol_CoaxialityTolerance.hxx>
68#include <StepDimTol_ConcentricityTolerance.hxx>
7fd59977 69#include <StepDimTol_CylindricityTolerance.hxx>
42cf5bc1 70#include <StepDimTol_Datum.hxx>
71#include <StepDimTol_DatumFeature.hxx>
72#include <StepDimTol_DatumReference.hxx>
6595eee7 73#include <StepDimTol_DatumReferenceElement.hxx>
74#include <StepDimTol_DatumSystem.hxx>
75#include <StepDimTol_DatumSystemOrReference.hxx>
76#include <StepDimTol_DatumTarget.hxx>
7fd59977 77#include <StepDimTol_FlatnessTolerance.hxx>
6595eee7 78#include <StepDimTol_GeometricToleranceType.hxx>
42cf5bc1 79#include <StepDimTol_GeometricToleranceWithDatumReference.hxx>
6595eee7 80#include <StepDimTol_GeometricToleranceWithModifiers.hxx>
81#include <StepDimTol_GeoTolAndGeoTolWthDatRef.hxx>
82#include <StepDimTol_GeoTolAndGeoTolWthDatRefAndGeoTolWthMaxTol.hxx>
83#include <StepDimTol_GeoTolAndGeoTolWthDatRefAndGeoTolWthMod.hxx>
42cf5bc1 84#include <StepDimTol_GeoTolAndGeoTolWthDatRefAndModGeoTolAndPosTol.hxx>
6595eee7 85#include <StepDimTol_GeoTolAndGeoTolWthMaxTol.hxx>
86#include <StepDimTol_GeoTolAndGeoTolWthMod.hxx>
42cf5bc1 87#include <StepDimTol_HArray1OfDatumReference.hxx>
6595eee7 88#include <StepDimTol_HArray1OfDatumReferenceElement.hxx>
89#include <StepDimTol_HArray1OfDatumReferenceModifier.hxx>
90#include <StepDimTol_HArray1OfDatumSystemOrReference.hxx>
7fd59977 91#include <StepDimTol_LineProfileTolerance.hxx>
42cf5bc1 92#include <StepDimTol_ModifiedGeometricTolerance.hxx>
93#include <StepDimTol_ParallelismTolerance.hxx>
94#include <StepDimTol_PerpendicularityTolerance.hxx>
6595eee7 95#include <StepDimTol_PlacedDatumTargetFeature.hxx>
7fd59977 96#include <StepDimTol_PositionTolerance.hxx>
97#include <StepDimTol_RoundnessTolerance.hxx>
6595eee7 98#include <StepDimTol_RunoutZoneDefinition.hxx>
99#include <StepDimTol_RunoutZoneOrientation.hxx>
7fd59977 100#include <StepDimTol_StraightnessTolerance.hxx>
101#include <StepDimTol_SurfaceProfileTolerance.hxx>
42cf5bc1 102#include <StepDimTol_SymmetryTolerance.hxx>
6595eee7 103#include <StepDimTol_ToleranceZone.hxx>
104#include <StepDimTol_ToleranceZoneForm.hxx>
42cf5bc1 105#include <StepDimTol_TotalRunoutTolerance.hxx>
6595eee7 106#include <StepGeom_Axis2Placement3d.hxx>
107#include <StepGeom_CartesianPoint.hxx>
108#include <StepGeom_Direction.hxx>
42cf5bc1 109#include <StepGeom_GeometricRepresentationContextAndGlobalUnitAssignedContext.hxx>
110#include <StepGeom_GeomRepContextAndGlobUnitAssCtxAndGlobUncertaintyAssCtx.hxx>
b0cef606 111#include <StepGeom_Plane.hxx>
42cf5bc1 112#include <StepGeom_Surface.hxx>
6595eee7 113#include <StepRepr_CompGroupShAspAndCompShAspAndDatumFeatAndShAsp.hxx>
114#include <StepRepr_CompositeShapeAspect.hxx>
b7b2f85a 115#include <StepRepr_ConstructiveGeometryRepresentation.hxx>
116#include <StepRepr_ConstructiveGeometryRepresentationRelationship.hxx>
117#include <StepRepr_DerivedShapeAspect.hxx>
7fd59977 118#include <StepRepr_DescriptiveRepresentationItem.hxx>
6595eee7 119#include <StepRepr_FeatureForDatumTargetRelationship.hxx>
42cf5bc1 120#include <StepRepr_HArray1OfRepresentationItem.hxx>
7fd59977 121#include <StepRepr_MeasureRepresentationItem.hxx>
42cf5bc1 122#include <StepRepr_NextAssemblyUsageOccurrence.hxx>
123#include <StepRepr_ProductDefinitionShape.hxx>
124#include <StepRepr_PropertyDefinition.hxx>
6595eee7 125#include <StepRepr_RepresentedDefinition.hxx>
42cf5bc1 126#include <StepRepr_Representation.hxx>
ec357c5c 127#include <StepRepr_RepresentationItem.hxx>
42cf5bc1 128#include <StepRepr_ReprItemAndLengthMeasureWithUnit.hxx>
6595eee7 129#include <StepRepr_ReprItemAndLengthMeasureWithUnitAndQRI.hxx>
130#include <StepRepr_ReprItemAndPlaneAngleMeasureWithUnit.hxx>
131#include <StepRepr_ReprItemAndPlaneAngleMeasureWithUnitAndQRI.hxx>
42cf5bc1 132#include <StepRepr_ShapeAspect.hxx>
b7b2f85a 133#include <StepRepr_ShapeAspectDerivingRelationship.hxx>
42cf5bc1 134#include <StepRepr_ShapeAspectRelationship.hxx>
135#include <StepRepr_SpecifiedHigherUsageOccurrence.hxx>
136#include <StepRepr_ValueRange.hxx>
137#include <StepShape_AdvancedFace.hxx>
6595eee7 138#include <StepShape_AngleRelator.hxx>
139#include <StepShape_AngularLocation.hxx>
140#include <StepShape_AngularSize.hxx>
42cf5bc1 141#include <StepShape_ConnectedFaceSet.hxx>
142#include <StepShape_ContextDependentShapeRepresentation.hxx>
6595eee7 143#include <StepShape_DimensionalCharacteristic.hxx>
42cf5bc1 144#include <StepShape_DimensionalCharacteristicRepresentation.hxx>
6595eee7 145#include <StepShape_DimensionalLocation.hxx>
146#include <StepShape_DimensionalLocationWithPath.hxx>
42cf5bc1 147#include <StepShape_DimensionalSize.hxx>
6595eee7 148#include <StepShape_DimensionalSizeWithPath.hxx>
42cf5bc1 149#include <StepShape_EdgeCurve.hxx>
150#include <StepShape_EdgeLoop.hxx>
151#include <StepShape_FaceBound.hxx>
6595eee7 152#include <StepShape_LimitsAndFits.hxx>
42cf5bc1 153#include <StepShape_OrientedEdge.hxx>
6595eee7 154#include <StepShape_PlusMinusTolerance.hxx>
155#include <StepShape_QualifiedRepresentationItem.hxx>
42cf5bc1 156#include <StepShape_ShapeDefinitionRepresentation.hxx>
157#include <StepShape_ShapeDimensionRepresentation.hxx>
158#include <StepShape_ShapeRepresentation.hxx>
6595eee7 159#include <StepShape_ShapeRepresentationWithParameters.hxx>
160#include <StepShape_ToleranceValue.hxx>
161#include <StepShape_TypeQualifier.hxx>
162#include <StepShape_ValueFormatTypeQualifier.hxx>
b0cef606 163#include <StepVisual_AnnotationPlane.hxx>
42cf5bc1 164#include <StepVisual_CurveStyle.hxx>
b0cef606 165#include <StepVisual_DraughtingCallout.hxx>
166#include <StepVisual_DraughtingModel.hxx>
42cf5bc1 167#include <StepVisual_HArray1OfInvisibleItem.hxx>
168#include <StepVisual_HArray1OfLayeredItem.hxx>
169#include <StepVisual_HArray1OfPresentationStyleAssignment.hxx>
170#include <StepVisual_HArray1OfPresentationStyleSelect.hxx>
171#include <StepVisual_Invisibility.hxx>
172#include <StepVisual_InvisibleItem.hxx>
173#include <StepVisual_MechanicalDesignGeometricPresentationRepresentation.hxx>
b0cef606 174#include <StepVisual_NullStyleMember.hxx>
42cf5bc1 175#include <StepVisual_PointStyle.hxx>
176#include <StepVisual_PresentationLayerAssignment.hxx>
ec357c5c 177#include <StepVisual_PresentationRepresentation.hxx>
42cf5bc1 178#include <StepVisual_PresentationStyleAssignment.hxx>
179#include <StepVisual_PresentationStyleByContext.hxx>
180#include <StepVisual_StyledItem.hxx>
181#include <StepVisual_SurfaceStyleUsage.hxx>
b0cef606 182#include <StepVisual_TessellatedAnnotationOccurrence.hxx>
183#include <StepVisual_TessellatedGeometricSet.hxx>
42cf5bc1 184#include <TCollection_AsciiString.hxx>
185#include <TCollection_HAsciiString.hxx>
186#include <TColStd_HArray1OfReal.hxx>
187#include <TColStd_HArray1OfTransient.hxx>
188#include <TColStd_HSequenceOfTransient.hxx>
6595eee7 189#include <TColStd_MapOfAsciiString.hxx>
42cf5bc1 190#include <TColStd_MapOfTransient.hxx>
191#include <TDataStd_Name.hxx>
192#include <TDataStd_TreeNode.hxx>
193#include <TDataStd_UAttribute.hxx>
194#include <TDF_AttributeSequence.hxx>
195#include <TDF_ChildIterator.hxx>
196#include <TDF_Label.hxx>
197#include <TDF_LabelSequence.hxx>
198#include <TDF_Tool.hxx>
199#include <TDocStd_Document.hxx>
200#include <TopoDS_Compound.hxx>
201#include <TopoDS_Iterator.hxx>
202#include <TopoDS_Shape.hxx>
203#include <TopTools_MapOfShape.hxx>
204#include <TopTools_SequenceOfShape.hxx>
205#include <Transfer_ActorOfFinderProcess.hxx>
206#include <Transfer_Binder.hxx>
207#include <Transfer_FinderProcess.hxx>
208#include <Transfer_TransientListBinder.hxx>
209#include <TransferBRep.hxx>
210#include <TransferBRep_ShapeMapper.hxx>
6595eee7 211#include <XCAFDimTolObjects_DatumObject.hxx>
212#include <XCAFDimTolObjects_DimensionFormVariance.hxx>
213#include <XCAFDimTolObjects_DimensionGrade.hxx>
214#include <XCAFDimTolObjects_DimensionObject.hxx>
215#include <XCAFDimTolObjects_DimensionModif.hxx>
216#include <XCAFDimTolObjects_DimensionModifiersSequence.hxx>
217#include <XCAFDimTolObjects_DimensionQualifier.hxx>
218#include <XCAFDimTolObjects_GeomToleranceObject.hxx>
42cf5bc1 219#include <XCAFDoc.hxx>
220#include <XCAFDoc_Area.hxx>
221#include <XCAFDoc_Centroid.hxx>
222#include <XCAFDoc_ColorTool.hxx>
223#include <XCAFDoc_Datum.hxx>
6595eee7 224#include <XCAFDoc_Dimension.hxx>
42cf5bc1 225#include <XCAFDoc_DimTol.hxx>
226#include <XCAFDoc_DimTolTool.hxx>
227#include <XCAFDoc_DocumentTool.hxx>
6595eee7 228#include <XCAFDoc_GeomTolerance.hxx>
42cf5bc1 229#include <XCAFDoc_GraphNode.hxx>
230#include <XCAFDoc_LayerTool.hxx>
231#include <XCAFDoc_Material.hxx>
232#include <XCAFDoc_MaterialTool.hxx>
233#include <XCAFDoc_ShapeTool.hxx>
234#include <XCAFDoc_Volume.hxx>
235#include <XCAFPrs.hxx>
236#include <XCAFPrs_DataMapIteratorOfDataMapOfStyleShape.hxx>
237#include <XCAFPrs_DataMapOfShapeStyle.hxx>
238#include <XCAFPrs_DataMapOfStyleShape.hxx>
239#include <XCAFPrs_Style.hxx>
240#include <XSControl_TransferWriter.hxx>
241#include <XSControl_WorkSession.hxx>
7fd59977 242
b0cef606 243static NCollection_Vector<Handle(StepVisual_AnnotationPlane)> gdtAnnotationPlanes;
244static Handle(StepVisual_DraughtingModel) gdtPresentationDM;
245static Handle(StepVisual_HArray1OfPresentationStyleAssignment) gdtPrsCurveStyle;
6595eee7 246
42cf5bc1 247// added by skl 15.01.2004 for D&GT writing
248//#include <StepRepr_CompoundItemDefinition.hxx>
249//#include <StepRepr_CompoundItemDefinitionMember.hxx>
250// added by skl 12.02.2004 for writing materials
7fd59977 251//=======================================================================
252//function : GetLabelName
253//purpose : auxilary function: take name of label and append it to str
254//=======================================================================
255static Standard_Boolean GetLabelName (const TDF_Label &L, Handle(TCollection_HAsciiString) &str)
256{
257 Handle(TDataStd_Name) N;
258 if ( ! L.FindAttribute ( TDataStd_Name::GetID(), N ) ) return Standard_False;
259 TCollection_ExtendedString name = N->Get();
260 if ( name.Length() <=0 ) return Standard_False;
261
262 // set name, converting it to Ascii and removing spaces
263 TCollection_AsciiString buf ( name, '?' );
264 buf.LeftAdjust();
265 buf.RightAdjust();
266 buf.ChangeAll(' ','_');
267 str->AssignCat ( buf.ToCString() );
268 return Standard_True;
269}
270
271
272//=======================================================================
273//function : STEPCAFControl_Writer
274//purpose :
275//=======================================================================
276
277STEPCAFControl_Writer::STEPCAFControl_Writer () :
278 myColorMode( Standard_True ),
279 myNameMode ( Standard_True ),
280 myLayerMode( Standard_True ),
281 myPropsMode( Standard_True ),
282 mySHUOMode ( Standard_True ),
283 myDGTMode ( Standard_True ),
284 myMatMode ( Standard_True )
285{
286 STEPCAFControl_Controller::Init();
287 Handle(XSControl_WorkSession) WS = new XSControl_WorkSession;
288 Init ( WS );
289}
290
291
292//=======================================================================
293//function : STEPCAFControl_Writer
294//purpose :
295//=======================================================================
296
297STEPCAFControl_Writer::STEPCAFControl_Writer (const Handle(XSControl_WorkSession)& WS,
298 const Standard_Boolean scratch)
299{
300 STEPCAFControl_Controller::Init();
301 Init ( WS, scratch );
302 myColorMode = Standard_True;
303 myNameMode = Standard_True;
304 myLayerMode = Standard_True;
305 myPropsMode = Standard_True;
306 mySHUOMode = Standard_True;
307}
308
309
310//=======================================================================
311//function : Init
312//purpose :
313//=======================================================================
314
315void STEPCAFControl_Writer::Init (const Handle(XSControl_WorkSession)& WS,
316 const Standard_Boolean scratch)
317{
318 WS->SelectNorm ( "STEP" );
319 myWriter.SetWS (WS,scratch);
320 myFiles = new STEPCAFControl_DictionaryOfExternFile;
321 myLabEF.Clear();
322 myLabels.Clear();
323}
324
325
326//=======================================================================
327//function : Write
328//purpose :
329//=======================================================================
330
331IFSelect_ReturnStatus STEPCAFControl_Writer::Write (const Standard_CString filename)
332{
333 IFSelect_ReturnStatus status = myWriter.Write ( filename );
334
335 // get directory name of the main file
336 OSD_Path mainfile ( filename );
337 mainfile.SetName ( "" );
338 mainfile.SetExtension ( "" );
339 TCollection_AsciiString dpath;
340 mainfile.SystemName ( dpath );
341
342 STEPCAFControl_IteratorOfDictionaryOfExternFile it ( myFiles );
343 for ( ; it.More(); it.Next() ) {
344 Handle(STEPCAFControl_ExternFile) EF = it.Value();
345 if ( EF->GetWriteStatus() != IFSelect_RetVoid ) continue;
346
347 // construct extern file name
348 TCollection_AsciiString fname = OSD_Path::AbsolutePath ( dpath, EF->GetName()->String() );
349 if ( fname.Length() <= 0 ) fname = EF->GetName()->String();
0797d9d3 350#ifdef OCCT_DEBUG
7fd59977 351 cout << "Writing external file: " << fname.ToCString() << endl;
352#endif
353
354 EF->SetWriteStatus ( EF->GetWS()->SendAll ( fname.ToCString() ) );
355 }
356
357 return status;
358}
359
360
361//=======================================================================
362//function : Transfer
363//purpose :
364//=======================================================================
365
1b44ab4b 366Standard_Boolean STEPCAFControl_Writer::Transfer( const Handle(TDocStd_Document) &doc,
7fd59977 367 const STEPControl_StepModelType mode,
1b44ab4b 368 const Standard_CString multi )
7fd59977 369{
370 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool( doc->Main() );
371 if ( STool.IsNull() ) return Standard_False;
372
373 TDF_LabelSequence labels;
374 STool->GetFreeShapes ( labels );
375 return Transfer ( myWriter, labels, mode, multi );
376}
377
378
379//=======================================================================
380//function : Transfer
381//purpose :
382//=======================================================================
383
1b44ab4b 384Standard_Boolean STEPCAFControl_Writer::Transfer( const TDF_Label& L,
7fd59977 385 const STEPControl_StepModelType mode,
1b44ab4b 386 const Standard_CString multi )
7fd59977 387{
388 TDF_LabelSequence labels;
389 labels.Append ( L );
390 return Transfer ( myWriter, labels, mode, multi );
391}
392
1b44ab4b 393//=======================================================================
394//function : Transfer
395//purpose :
396//=======================================================================
397
398Standard_Boolean STEPCAFControl_Writer::Transfer( const TDF_LabelSequence& labels,
399 const STEPControl_StepModelType mode,
400 const Standard_CString multi )
401{
402 return Transfer( myWriter, labels, mode, multi );
403}
7fd59977 404
405//=======================================================================
406//function : Perform
407//purpose :
408//=======================================================================
409
410Standard_Boolean STEPCAFControl_Writer::Perform (const Handle(TDocStd_Document) &doc,
411 const Standard_CString filename)
412{
413 if ( ! Transfer ( doc ) ) return Standard_False;
414 return Write ( filename ) == IFSelect_RetDone;
415}
416
417
418//=======================================================================
419//function : Perform
420//purpose :
421//=======================================================================
422
423Standard_Boolean STEPCAFControl_Writer::Perform (const Handle(TDocStd_Document) &doc,
424 const TCollection_AsciiString &filename)
425{
426 if ( ! Transfer ( doc ) ) return Standard_False;
427 return Write ( filename.ToCString() ) == IFSelect_RetDone;
428}
429
430
431//=======================================================================
432//function : ExternFiles
433//purpose :
434//=======================================================================
435
436const Handle(STEPCAFControl_DictionaryOfExternFile) &STEPCAFControl_Writer::ExternFiles () const
437{
438 return myFiles;
439}
440
441
442//=======================================================================
443//function : ExternFile
444//purpose :
445//=======================================================================
446
447Standard_Boolean STEPCAFControl_Writer::ExternFile (const TDF_Label &L,
448 Handle(STEPCAFControl_ExternFile) &ef) const
449{
450 ef.Nullify();
451 if ( ! myLabEF.IsBound ( L ) ) return Standard_False;
452 ef = myLabEF.Find ( L );
453 return Standard_True;
454}
455
456
457//=======================================================================
458//function : ExternFile
459//purpose :
460//=======================================================================
461
462Standard_Boolean STEPCAFControl_Writer::ExternFile (const Standard_CString name,
463 Handle(STEPCAFControl_ExternFile) &ef) const
464{
465 ef.Nullify();
466 if ( ! myFiles.IsNull() || ! myFiles->HasItem ( name ) )
467 return Standard_False;
468 ef = myFiles->Item ( name );
469 return Standard_True;
470}
471
472
473//=======================================================================
474//function : Writer
475//purpose :
476//=======================================================================
477
478STEPControl_Writer &STEPCAFControl_Writer::ChangeWriter ()
479{
480 return myWriter;
481}
482
483
484//=======================================================================
485//function : Writer
486//purpose :
487//=======================================================================
488
489const STEPControl_Writer &STEPCAFControl_Writer::Writer () const
490{
491 return myWriter;
492}
493
494
495//=======================================================================
496//function : Transfer
497//purpose :
498//=======================================================================
499
500Standard_Boolean STEPCAFControl_Writer::Transfer (STEPControl_Writer &writer,
501 const TDF_LabelSequence &labels,
502 const STEPControl_StepModelType mode,
503 const Standard_CString multi,
504 const Standard_Boolean isExternFile)
505{
506 if ( labels.Length() <=0 ) return Standard_False;
507
508 Handle(STEPCAFControl_ActorWrite) Actor =
509 Handle(STEPCAFControl_ActorWrite)::DownCast ( writer.WS()->NormAdaptor()->ActorWrite() );
510
511 // translate free top-level shapes of the DECAF document
7fd59977 512 Standard_Integer ap = Interface_Static::IVal ("write.step.schema");
513 TDF_LabelSequence sublabels;
514 for ( Standard_Integer i=1; i <= labels.Length(); i++ ) {
515 TDF_Label L = labels.Value(i);
516 TopoDS_Shape dummy;
517 if ( myLabels.IsBound ( L ) ) continue; // already processed
518
519 TopoDS_Shape shape = XCAFDoc_ShapeTool::GetShape ( L );
520 if ( shape.IsNull() ) continue;
521
522 // write shape either as a whole, or as multifile (with extern refs)
f5e92b04 523 if ( ! multi ) {
7fd59977 524 Actor->SetStdMode ( Standard_False );
525
7fd59977 526 TDF_LabelSequence comp;
f5e92b04 527
528 //for case when only part of assemby structure should be written in the document
529 //if specified label is component of the assembly then
530 //in order to save location of this component in the high-level assembly
531 //and save name of high-level assembly it is necessary to represent structure of high-level assembly
532 //as assembly with one component specified by current label.
533 //For that compound containing only specified component is binded to the label of the high-level assembly.
534 //The such way full structure of high-level assembly was replaced on the assembly contaning one component.
535 if ( XCAFDoc_ShapeTool::IsComponent ( L ) )
536 {
537 TopoDS_Compound aComp;
538 BRep_Builder aB;
539 aB.MakeCompound(aComp);
540 aB.Add(aComp, shape);
541 shape = aComp;
542 comp.Append(L);
543 TDF_Label ref;
544 if ( XCAFDoc_ShapeTool::GetReferredShape ( L, ref ) )
545 {
546 if(XCAFDoc_ShapeTool::IsAssembly ( ref))
547 XCAFDoc_ShapeTool::GetComponents ( ref, comp, Standard_True );
548 }
549 L = L.Father();
550 }
551 else
552 {
553 // fill sequence of (sub) shapes for which attributes should be written
554 // and set actor to handle assemblies in a proper way
555 if(XCAFDoc_ShapeTool::IsAssembly ( L ))
556 XCAFDoc_ShapeTool::GetComponents ( L, comp, Standard_True );
557 }
558
7fd59977 559 for ( Standard_Integer k=1; k <= comp.Length(); k++ ) {
f5e92b04 560 TDF_Label ref;
561 if ( ! XCAFDoc_ShapeTool::GetReferredShape ( comp(k), ref ) ) continue;
562 if ( ! myLabels.IsBound ( ref ) ) {
563 TopoDS_Shape refS = XCAFDoc_ShapeTool::GetShape ( ref );
564 myLabels.Bind ( ref, refS );
565 sublabels.Append ( ref );
566 if ( XCAFDoc_ShapeTool::IsAssembly ( ref ) )
567 Actor->RegisterAssembly ( refS );
568 }
7fd59977 569 }
570 myLabels.Bind ( L, shape );
571 sublabels.Append ( L );
572 if ( XCAFDoc_ShapeTool::IsAssembly ( L ) )
f5e92b04 573 Actor->RegisterAssembly ( shape );
7fd59977 574
575 writer.Transfer(shape,mode,Standard_False);
576 Actor->SetStdMode ( Standard_True ); // restore default behaviour
577 }
578 else {
579 // translate final solids
580 TopoDS_Shape Sass = TransferExternFiles ( L, mode, sublabels, multi );
581
582 // translate main assembly structure
583/*
584 if ( ap == 3 ) { // if AP203, switch to AP214
585 Interface_Static::SetCVal ("write.step.schema", "AP214DIS");
586 Handle(StepData_StepModel) model =
587 Handle(StepData_StepModel)::DownCast ( writer.WS()->Model() );
588 if ( model->HasHeaderEntity(STANDARD_TYPE(HeaderSection_FileSchema)) ) {
589 Handle(HeaderSection_FileSchema) fs =
590 Handle(HeaderSection_FileSchema)::DownCast ( model->HeaderEntity(STANDARD_TYPE(HeaderSection_FileSchema)) );
591 Handle(TCollection_HAsciiString) str = fs->SchemaIdentifiersValue ( 1 );
592 Handle(TCollection_HAsciiString) ap214 = new TCollection_HAsciiString ( "AUTOMOTIVE_DESIGN" );
593 if ( str->Search ( ap214 ) <0 ) {
594 str->Clear();
595 str->AssignCat ( ap214 );
596 }
597 }
598 }
599*/
600 Standard_Integer assemblymode = Interface_Static::IVal ("write.step.assembly");
601 Interface_Static::SetCVal ("write.step.assembly", "On");
602 writer.Transfer ( Sass, STEPControl_AsIs );
603 Interface_Static::SetIVal ("write.step.assembly", assemblymode);
604 Interface_Static::SetIVal ("write.step.schema", ap);
605 }
606 }
607
608 writer.WS()->ComputeGraph(Standard_True );// added by skl 03.11.2003 since we use
609 // writer.Transfer() wihtout compute graph
610
611 // write names
612 if ( GetNameMode() )
613 WriteNames ( writer.WS(), sublabels );
614
615 if ( !multi ) {
616 // write colors
617 if ( GetColorMode() )
618 WriteColors ( writer.WS(), sublabels );
619
620 // write layers
621 if ( GetLayerMode() )
622 WriteLayers ( writer.WS(), sublabels );
623
624 // write SHUO entities
625 if ( GetSHUOMode() && !isExternFile )
626 // do not store SHUO for extern reference for the moment
627 WriteSHUOs ( writer.WS(), sublabels );
628
629 // write G&DTs
d658f275 630 if(GetDimTolMode()) {
631 if (ap == 5) {
632 WriteDGTsAP242(writer.WS(), sublabels);
633 }
634 else {
635 WriteDGTs(writer.WS(), sublabels);
636 }
637 }
7fd59977 638
639 // write Materials
640 if(GetMaterialMode())
641 WriteMaterials(writer.WS(),sublabels);
642
643 // register all MDGPRs in model
7f56eba8 644 const Handle(Interface_InterfaceModel) &Model = writer.WS()->Model();
7fd59977 645 MoniTool_DataMapIteratorOfDataMapOfShapeTransient anItr(myMapCompMDGPR);
7f56eba8 646 for (; anItr.More(); anItr.Next())
7fd59977 647 Model->AddWithRefs( anItr.Value() );
7fd59977 648 }
649
650 if ( multi ) { // external refs
651 WriteExternRefs ( writer.WS(), sublabels );
652 }
653
654 // write validation props
655// if ( multi && ap ==3 ) {
656// Interface_Static::SetCVal ("write.step.schema", "AP214DIS");
657// }
658 if ( GetPropsMode() )
659 WriteValProps ( writer.WS(), sublabels, multi );
660
661 Interface_Static::SetIVal ("write.step.schema", ap);
662
663 // refresh graph
664 writer.WS()->ComputeGraph ( Standard_True );
665
02a0b964 666 /* ================================
667 * Write names for the sub-shapes
668 * ================================ */
669
dde68833 670 if (Interface_Static::IVal("write.stepcaf.subshapes.name") != 0)
02a0b964 671 {
7f56eba8 672 const Handle(XSControl_TransferWriter) &TW = this->ChangeWriter().WS()->TransferWriter();
673 const Handle(Transfer_FinderProcess) &FP = TW->FinderProcess();
02a0b964 674
675 for ( int i = 1; i <= labels.Length(); i++ )
676 {
677 TDF_Label L = labels.Value(i);
678
679 for ( TDF_ChildIterator it(L, Standard_True); it.More(); it.Next() )
680 {
681 TDF_Label SubL = it.Value();
682
683 // Access name recorded in OCAF TDataStd_Name attribute
684 Handle(TCollection_HAsciiString) hSubName = new TCollection_HAsciiString;
685 if ( !GetLabelName(SubL, hSubName) )
686 continue;
687
688 // Access topological data
689 TopoDS_Shape SubS = XCAFDoc_ShapeTool::GetShape(SubL);
690 if ( SubS.IsNull() )
691 continue;
692
693 // Access the correspondent STEP Representation Item
694 Handle(StepRepr_RepresentationItem) RI;
695 Handle(TransferBRep_ShapeMapper) aShMapper = TransferBRep::ShapeMapper(FP, SubS);
696 if ( !FP->FindTypedTransient(aShMapper, STANDARD_TYPE(StepRepr_RepresentationItem), RI) )
697 continue;
698
699 // Record the name
700 RI->SetName(hSubName);
701 }
702 }
703 }
704
7fd59977 705 return Standard_True;
706}
707
708
709//=======================================================================
710//function : TransferExternFiles
711//purpose :
712//=======================================================================
713
714TopoDS_Shape STEPCAFControl_Writer::TransferExternFiles (const TDF_Label &L,
715 const STEPControl_StepModelType mode,
716 TDF_LabelSequence &labels,
717 const Standard_CString prefix)
718{
719 // if label already translated, just return the shape
720 if ( myLabels.IsBound ( L ) ) {
721 return myLabels.Find ( L );
722 }
723
724 TopoDS_Compound C;
725 BRep_Builder B;
726 B.MakeCompound ( C );
f5e92b04 727 //labels.Append ( L );
7fd59977 728 // if not assembly, write to separate file
f5e92b04 729 if ( ! XCAFDoc_ShapeTool::IsAssembly ( L ) && !XCAFDoc_ShapeTool::IsComponent ( L )) {
730 labels.Append ( L );
7fd59977 731 // prepare for transfer
732 Handle(XSControl_WorkSession) newWS = new XSControl_WorkSession;
733 newWS->SelectNorm ( "STEP" );
734 STEPControl_Writer sw ( newWS, Standard_True );
735 TDF_LabelSequence Lseq;
736 Lseq.Append ( L );
737
738 // construct the name for extern file
739 Handle(TCollection_HAsciiString) basename = new TCollection_HAsciiString;
740 if ( prefix && prefix[0] ) basename->AssignCat ( prefix );
741 GetLabelName ( L, basename );
742 Handle(TCollection_HAsciiString) name = new TCollection_HAsciiString ( basename );
743 name->AssignCat ( ".stp" );
744 if ( myFiles->HasItem ( name->ToCString() ) ) { // avoid confusions
745 for ( Standard_Integer k=1; k < 32000; k++ ) {
746 name = new TCollection_HAsciiString ( basename );
747 name->AssignCat ( "_" );
748 name->AssignCat ( TCollection_AsciiString ( k ).ToCString() );
749 name->AssignCat ( ".stp" );
750 if ( ! myFiles->HasItem ( name->ToCString() ) ) break;
751 }
752 }
753
754 // translate and record extern file
755 Handle(STEPCAFControl_ExternFile) EF = new STEPCAFControl_ExternFile;
756 EF->SetWS ( newWS );
757 EF->SetName ( name );
758 EF->SetLabel ( L );
759 Standard_Integer assemblymode = Interface_Static::IVal ("write.step.assembly");
760 Interface_Static::SetCVal ("write.step.assembly", "Off");
761 const Standard_CString multi = 0;
762 EF->SetTransferStatus ( Transfer ( sw, Lseq, mode, multi, Standard_True ) );
763 Interface_Static::SetIVal ("write.step.assembly", assemblymode);
764 myLabEF.Bind ( L, EF );
765 myFiles->SetItem ( name->ToCString(), EF );
766
767 // return empty compound as replacement for the shape
768 myLabels.Bind ( L, C );
769 return C;
770 }
f5e92b04 771 TDF_LabelSequence comp;
772 TDF_Label aCurL = L;
773 //if specified shape is component then high-level assembly is considered
774 //to get valid structure with location
775 if ( XCAFDoc_ShapeTool::IsComponent ( L ) )
776 {
777 comp.Append(L);
778 aCurL = L.Father();
779 }
7fd59977 780 // else iterate on components add create structure of empty compounds
781 // representing the assembly
f5e92b04 782 else if (XCAFDoc_ShapeTool::IsAssembly ( L ))
783 XCAFDoc_ShapeTool::GetComponents ( L, comp, Standard_False );
784
785 labels.Append ( aCurL );
7fd59977 786 for ( Standard_Integer k=1; k <= comp.Length(); k++ ) {
787 TDF_Label lab = comp(k);
788 TDF_Label ref;
789 if ( ! XCAFDoc_ShapeTool::GetReferredShape ( lab, ref ) ) continue;
790 TopoDS_Shape Scomp = TransferExternFiles ( ref, mode, labels, prefix );
791 Scomp.Location ( XCAFDoc_ShapeTool::GetLocation ( lab ) );
792 B.Add ( C, Scomp );
793 }
f5e92b04 794 myLabels.Bind ( aCurL, C );
7fd59977 795 return C;
796}
797
798
799//=======================================================================
800//function : WriteExternRefs
801//purpose :
802//=======================================================================
803
804Standard_Boolean STEPCAFControl_Writer::WriteExternRefs (const Handle(XSControl_WorkSession) &WS,
805 const TDF_LabelSequence &labels) const
806{
807 if ( labels.Length() <=0 ) return Standard_False;
808
7f56eba8 809 const Handle(XSControl_TransferWriter) &TW = WS->TransferWriter();
810 const Handle(Transfer_FinderProcess) &FP = TW->FinderProcess();
7fd59977 811 STEPConstruct_ExternRefs EFTool ( WS );
812 Standard_Integer schema = Interface_Static::IVal("write.step.schema");
813 for ( Standard_Integer k=1; k <= labels.Length(); k++ ) {
814 TDF_Label lab = labels(k);
815 if ( XCAFDoc_ShapeTool::IsAssembly ( lab ) ) continue; // skip assemblies
816
817 // get extern file
818 Handle(STEPCAFControl_ExternFile) EF;
819 if ( ! ExternFile ( lab, EF ) ) continue; // should never be
820
821 // find SDR
822 if ( ! myLabels.IsBound ( lab ) ) continue; // not recorded as translated, skip
823 TopoDS_Shape S = myLabels.Find ( lab );
824
825 Handle(StepShape_ShapeDefinitionRepresentation) SDR;
826 Handle(TransferBRep_ShapeMapper) mapper = TransferBRep::ShapeMapper ( FP, S );
827 if ( ! FP->FindTypedTransient ( mapper, STANDARD_TYPE(StepShape_ShapeDefinitionRepresentation), SDR ) ) {
0797d9d3 828#ifdef OCCT_DEBUG
7fd59977 829 cout << "Warning: Cannot find SDR for " << S.TShape()->DynamicType()->Name() << endl;
830#endif
831 continue;
832 }
833
834 // add extern ref
835 const Standard_CString format = (const Standard_CString) ( schema == 3 ? "STEP AP203" : "STEP AP214" );
836 // try to get PD from SDR
837 StepRepr_RepresentedDefinition RD = SDR->Definition();
838 Handle(StepRepr_PropertyDefinition) aPropDef = RD.PropertyDefinition();
839 if (aPropDef.IsNull()) {
0797d9d3 840#ifdef OCCT_DEBUG
7fd59977 841 cout << "Warning: STEPCAFControl_Writer::WriteExternRefs StepRepr_PropertyDefinition is null for " << S.TShape()->DynamicType()->Name() << endl;
842#endif
843 continue;
844 }
845 StepRepr_CharacterizedDefinition CharDef = aPropDef->Definition();
846 Handle(StepBasic_ProductDefinition) PD = CharDef.ProductDefinition();
847 if (PD.IsNull()) {
0797d9d3 848#ifdef OCCT_DEBUG
7fd59977 849 cout << "Warning: STEPCAFControl_Writer::WriteExternRefs StepBasic_ProductDefinition is null for " << S.TShape()->DynamicType()->Name() << endl;
850#endif
851 continue;
852 }
853 EFTool.AddExternRef ( EF->GetName()->ToCString(), PD, format );
854 }
855 EFTool.WriteExternRefs(schema);
856 return Standard_True;
857}
858
859
860//=======================================================================
861//function : FindEntities
862//purpose : auxilary
863//=======================================================================
864static Standard_Integer FindEntities (const Handle(Transfer_FinderProcess) &FP,
865 const TopoDS_Shape &S,
866 TopLoc_Location &L,
867 TColStd_SequenceOfTransient &seqRI)
868{
869 Handle(StepRepr_RepresentationItem) item = STEPConstruct::FindEntity ( FP, S, L );
870
871 if ( ! item.IsNull() ) {
872 seqRI.Append ( item );
873 return 1;
874 }
875
876 // may be S was splited during shape processing
877 Handle(TransferBRep_ShapeMapper) mapper = TransferBRep::ShapeMapper ( FP, S );
878 Handle(Transfer_Binder) bnd = FP->Find ( mapper );
879 if ( bnd.IsNull() ) return 0;
880
881 Handle(Transfer_TransientListBinder) TransientListBinder =
882 //Handle(Transfer_TransientListBinder)::DownCast( bnd->Next(Standard_True) );
883 Handle(Transfer_TransientListBinder)::DownCast( bnd );
884 Standard_Integer nres=0;
885 if ( TransientListBinder.IsNull() && S.ShapeType() == TopAbs_COMPOUND)
886 {
887 for ( TopoDS_Iterator it(S); it.More(); it.Next() ) {
51740958 888 Handle(StepRepr_RepresentationItem) aLocalItem = STEPConstruct::FindEntity ( FP, it.Value(), L );
889 if (aLocalItem.IsNull() ) continue;
7fd59977 890 nres++;
51740958 891 seqRI.Append (aLocalItem);
7fd59977 892 }
893 }
894 else
895 {
896 const Standard_Integer nb = TransientListBinder->NbTransients();
897 for (Standard_Integer i=1; i<=nb; i++) {
898 Handle(Standard_Transient) t = TransientListBinder->Transient(i);
899 item = Handle(StepRepr_RepresentationItem)::DownCast(t);
900 if ( item.IsNull() ) continue;
901 nres++;
902 seqRI.Append ( item );
903 }
904 }
905/* works but is obsolete: another approach
906 if (i<=nb) {
907 TopoDS_Shape comp = TransferBRep::ShapeResult(bnd);
908 if ( ! comp.IsNull() && comp.ShapeType() < S.ShapeType() ) {
909 for ( TopoDS_Iterator it(comp); it.More(); it.Next() ) {
910 MakeSTEPStyles(Styles, it.Value(), settings, STEPstyle,
911 Map, ( hasOwn ? &style : 0 ) );
912 }
913 }
914 }
915*/
916 return nres;
917}
918
919
920//=======================================================================
921//function : getStyledItem
922//purpose : auxilary
923//=======================================================================
924static Standard_Boolean getStyledItem(const TopoDS_Shape& S,
925 const Handle(XCAFDoc_ShapeTool)& STool,
926 const STEPConstruct_Styles &Styles,
927 Handle(StepVisual_StyledItem) &resSelItem,
928 const MoniTool_DataMapOfShapeTransient& myMapCompMDGPR)
929{
930 TDF_Label aTopShL = STool->FindShape(S, Standard_False);
931 TopoDS_Shape aTopLevSh = STool->GetShape( aTopShL );
932 Standard_Boolean found = Standard_False;
933 if ( !aTopLevSh.IsNull() && myMapCompMDGPR.IsBound( aTopLevSh ) ) {
934 Handle(StepVisual_PresentationRepresentation) aMDGPR =
935 Handle(StepVisual_PresentationRepresentation)::DownCast( myMapCompMDGPR.Find( aTopLevSh ) );
936 Handle(StepRepr_HArray1OfRepresentationItem) anSelItmHArr = aMDGPR->Items();
937 // search for PSA of Monifold solid
938 if ( !anSelItmHArr.IsNull() )
939 {
940 for (Standard_Integer si = 1; si <= anSelItmHArr->Length(); si++) {
941 Handle(StepVisual_StyledItem) aSelItm =
942 Handle(StepVisual_StyledItem)::DownCast(anSelItmHArr->Value(si));
943
944 if ( aSelItm.IsNull() )
945 continue;
946
947 // check that it is a stiled item for monifold solid brep
948 TopLoc_Location Loc;
949 TColStd_SequenceOfTransient aNewseqRI;
950 FindEntities ( Styles.FinderProcess(), aTopLevSh, Loc, aNewseqRI );
951 if ( aNewseqRI.Length() > 0 )
952 {
953
954 Handle(StepRepr_RepresentationItem) anItem = aSelItm->Item();
955 Standard_Boolean isSameMonSolBR = Standard_False;
956 for (Standard_Integer mi = 1; mi <= aNewseqRI.Length(); mi++) {
957 if ( !anItem.IsNull() && anItem == aNewseqRI.Value( mi ) ) {
958 isSameMonSolBR = Standard_True;
959 break;
960 }
961 }
962 if (!isSameMonSolBR)
963 continue;
964 }
965
966
967 for (Standard_Integer jsi = 1; jsi <= aSelItm->NbStyles() && !found; jsi++) {
a9dde4a3 968 Handle(StepVisual_PresentationStyleAssignment) aFatherPSA = aSelItm->StylesValue(jsi);
7fd59977 969 // check for PSA for top-level (not Presentation style by contex for NAUO)
970 if (aFatherPSA.IsNull() || aFatherPSA->IsKind(STANDARD_TYPE(StepVisual_PresentationStyleByContext)))
971 continue;
972 resSelItem = aSelItm;
973 found = Standard_True;
974 }
975 }
976 }
977 }
978 return found;
979}
980
981
982//=======================================================================
983//function : setDefaultInstanceColor
984//purpose : auxilary
985//=======================================================================
986static Standard_Boolean setDefaultInstanceColor (const Handle(StepVisual_StyledItem) &aSelItm,
987 Handle(StepVisual_PresentationStyleAssignment)& PSA)
988{
989 Standard_Boolean found = Standard_False;
990 for (Standard_Integer jsi = 1; jsi <= aSelItm->NbStyles() && !found; jsi++) {
a9dde4a3 991 Handle(StepVisual_PresentationStyleAssignment) aFatherPSA = aSelItm->StylesValue(jsi);
7fd59977 992 // check for PSA for top-level (not Presentation style by contex for NAUO)
993 if (aFatherPSA.IsNull() || aFatherPSA->IsKind(STANDARD_TYPE(StepVisual_PresentationStyleByContext)))
994 return Standard_False;
995
996 // get style select from father PSA
997 if (aFatherPSA->NbStyles() > 0) {
998 Handle(StepVisual_HArray1OfPresentationStyleSelect) aFatherStyles =
999 new StepVisual_HArray1OfPresentationStyleSelect(1, aFatherPSA->NbStyles());
1000 for (Standard_Integer k = 1; k <= aFatherPSA->NbStyles(); k++) {
1001 StepVisual_PresentationStyleSelect PSS;
1002 StepVisual_PresentationStyleSelect olDPSS = aFatherPSA->StylesValue(k);
1003 if (!olDPSS.PointStyle().IsNull())
1004 PSS.SetValue (olDPSS.PointStyle());
1005 else if (!olDPSS.CurveStyle().IsNull())
1006 PSS.SetValue (olDPSS.CurveStyle());
1007 else if (!olDPSS.SurfaceStyleUsage().IsNull())
1008 PSS.SetValue (olDPSS.SurfaceStyleUsage());
1009 else {
1010 found = Standard_False;
1011 break;
1012 }
1013 //aFatherStyles->SetValue( k, PSS );
1014 aFatherStyles->SetValue( k, olDPSS );
1015 found = Standard_True;
1016 }
1017 // init PSA of NAUO
1018 if (found) {
1019 PSA->Init( aFatherStyles );
1020 }
1021 }
1022
1023 }
1024 return found;
1025}
1026
1027
1028//=======================================================================
1029//function : MakeSTEPStyles
1030//purpose : auxilary
1031//=======================================================================
1032static void MakeSTEPStyles (STEPConstruct_Styles &Styles,
1033 const TopoDS_Shape &S,
1034 const XCAFPrs_DataMapOfShapeStyle &settings,
1035 Handle(StepVisual_StyledItem) &override,
1036 TopTools_MapOfShape &Map,
1037 const MoniTool_DataMapOfShapeTransient& myMapCompMDGPR,
1038 STEPConstruct_DataMapOfAsciiStringTransient &DPDCs,
1039 STEPConstruct_DataMapOfPointTransient &ColRGBs,
1040 const Handle(XCAFDoc_ColorTool)& CTool,
1041 const XCAFPrs_Style *inherit = 0,
1042 const Standard_Boolean isComponent = Standard_False)
1043{
1044 // skip already processed shapes
1045 if ( ! Map.Add ( S ) ) return;
1046
1047 // check if shape has its own style (r inherits from ancestor)
1048 XCAFPrs_Style style;
1049 if ( inherit ) style = *inherit;
1050 if ( settings.IsBound(S) ) {
1051 XCAFPrs_Style own = settings.Find(S);
1052 if ( !own.IsVisible() ) style.SetVisibility ( Standard_False );
1053 if ( own.IsSetColorCurv() ) style.SetColorCurv ( own.GetColorCurv() );
1054 if ( own.IsSetColorSurf() ) style.SetColorSurf ( own.GetColorSurf() );
1055 }
1056
1057 // translate colors to STEP
1058 Handle(StepVisual_Colour) surfColor, curvColor;
1059 if ( style.IsSetColorSurf() )
1060 surfColor = Styles.EncodeColor(style.GetColorSurf(),DPDCs,ColRGBs);
1061 if ( style.IsSetColorCurv() )
1062 curvColor = Styles.EncodeColor(style.GetColorCurv(),DPDCs,ColRGBs);
1063
1064 Standard_Boolean hasOwn = ( ! surfColor.IsNull() ||
1065 ! curvColor.IsNull() ||
1066 ! style.IsVisible() );
1067
1068 // find target item and assign style to it
1069 Handle(StepVisual_StyledItem) STEPstyle = override;
1070 if ( hasOwn ) {
1071 if ( S.ShapeType() != TopAbs_COMPOUND || isComponent ) { // skip compounds, let subshapes inherit its colors
1072 TopLoc_Location L;
1073 TColStd_SequenceOfTransient seqRI;
1074 Standard_Integer nb = FindEntities ( Styles.FinderProcess(), S, L, seqRI );
0797d9d3 1075#ifdef OCCT_DEBUG
7fd59977 1076 if ( nb <=0 ) cout << "Warning: Cannot find RI for " << S.TShape()->DynamicType()->Name() << endl;
1077#endif
1078 //Get overridden style gka 10.06.03
1079 if ( isComponent && nb)
1080 getStyledItem(S, CTool->ShapeTool(), Styles, override,myMapCompMDGPR);
1081
1082
1083 for ( Standard_Integer i=1; i <= nb; i++ ) {
1084 Handle(StepRepr_RepresentationItem) item =
1085 Handle(StepRepr_RepresentationItem)::DownCast(seqRI(i));
1086 Handle(StepVisual_PresentationStyleAssignment) PSA;
1087 if ( style.IsVisible() || !surfColor.IsNull() || !curvColor.IsNull() ) {
1088 PSA = Styles.MakeColorPSA ( item, surfColor, curvColor, isComponent );
1089 }
1090 else {
1091 // default white color
1092 surfColor = Styles.EncodeColor(Quantity_Color(1,1,1,Quantity_TOC_RGB),DPDCs,ColRGBs);
1093 PSA = Styles.MakeColorPSA ( item, surfColor, curvColor, isComponent );
1094 if ( isComponent )
1095 setDefaultInstanceColor( override, PSA);
1096
1097 } // end of component case
1098
1099 STEPstyle = Styles.AddStyle ( item, PSA, override );
1100 hasOwn = Standard_False;
1101 }
1102 }
1103 }
1104
1105 // iterate on subshapes (except vertices :)
1106 if ( S.ShapeType() == TopAbs_EDGE ) return;
1107 if ( !isComponent ) // PTV 10.02.2003
1108 for ( TopoDS_Iterator it(S); it.More(); it.Next() ) {
1109 MakeSTEPStyles ( Styles, it.Value(), settings, STEPstyle,
1110 Map, myMapCompMDGPR, DPDCs, ColRGBs, CTool,
1111 ( hasOwn ? &style : 0 ) );
1112 }
1113}
1114
1115/*
1116static Standard_Boolean getFatherColor (const TDF_Label& L,
1117 const Handle(XCAFDoc_ColorTool)& CTool,
1118 XCAFPrs_Style& style)
1119{
1120 Standard_Boolean done = Standard_False;
1121 TopoDS_Shape aSh = CTool->ShapeTool()->GetShape( L );
1122 TDF_Label aFL = CTool->ShapeTool()->FindShape( aSh );
1123 if (aFL.IsNull() || aFL == L)
1124 return done;
1125 Quantity_Color C;
1126 if ( CTool->GetColor ( aFL, XCAFDoc_ColorGen, C ) ) {
1127 style.SetColorCurv ( C );
1128 style.SetColorSurf ( C );
1129 done = Standard_True;
1130 }
1131 if ( CTool->GetColor ( aFL, XCAFDoc_ColorSurf, C ) ) {
1132 style.SetColorSurf ( C );
1133 done = Standard_True;
1134 }
1135 if ( CTool->GetColor ( aFL, XCAFDoc_ColorCurv, C ) ) {
1136 style.SetColorCurv ( C );
1137 done = Standard_True;
1138 }
1139
1140 return done;
1141}
1142*/
1143
1144
1145//=======================================================================
1146//function : WriteColors
1147//purpose :
1148//=======================================================================
1149
1150Standard_Boolean STEPCAFControl_Writer::WriteColors (const Handle(XSControl_WorkSession) &WS,
1151 const TDF_LabelSequence &labels)
1152{
1153 if ( labels.Length() <=0 ) return Standard_False;
1154
1155 // Iterate on shapes in the document
1156 Handle(XCAFDoc_ColorTool) CTool = XCAFDoc_DocumentTool::ColorTool( labels(1) );
1157 if ( CTool.IsNull() ) return Standard_False;
1158
1159 STEPConstruct_Styles Styles ( WS );
1160 STEPConstruct_DataMapOfAsciiStringTransient DPDCs;
1161 STEPConstruct_DataMapOfPointTransient ColRGBs;
1162 for ( Standard_Integer i=1; i <= labels.Length(); i++ ) {
1163 TDF_Label L = labels.Value(i);
1164
1165 Handle(XCAFDoc_ShapeTool) aSTool = XCAFDoc_DocumentTool::ShapeTool( labels(1) );
1166 // Skip assemblies: colors assigned to assemblies and their instances
1167 // are not supported (it is not clear how to encode that in STEP)
1168 if ( XCAFDoc_ShapeTool::IsAssembly ( L ) ) {
0797d9d3 1169#ifdef OCCT_DEBUG
7fd59977 1170 cout << "Warning: Cannot write color for Assembly" << endl;
1171 cout << "Info: Check for colors assigned to components in assembly" << endl;
1172#endif
1173 // PTV 22.01.2003 Write color for instances.
1174 TDF_LabelSequence compLabels;
1175 if ( aSTool.IsNull() )
1176 continue;
1177 if (!aSTool->GetComponents(L, compLabels))
1178 continue;
1179 WriteColors(WS, compLabels);
1180 continue;
1181 }
1182 Styles.ClearStyles();
1183
1184 // get a target shape and try to find corresponding context
1185 // (all the colors set under that label will be put into that context)
1186 TopoDS_Shape S;
1187 if ( ! XCAFDoc_ShapeTool::GetShape ( L, S ) ) continue;
1188 Standard_Boolean isComponent = aSTool->IsComponent( L );
1189 TopoDS_Shape aTopSh = S;
1190 Handle(StepRepr_RepresentationContext) Context = Styles.FindContext ( S );
1191 if ( isComponent ) {
1192 TDF_Label aTopShL = aSTool->FindShape(S, Standard_False);
1193 if (aTopShL.IsNull())
1194 continue;
1195 aTopSh = aSTool->GetShape( aTopShL );
1196 Context = Styles.FindContext ( aTopSh );
1197 }
1198 if ( Context.IsNull() )
1199 continue;
1200
1201 // collect settings set on that label
1202 XCAFPrs_DataMapOfShapeStyle settings;
1203 TDF_LabelSequence seq;
1204 seq.Append ( L );
1205 XCAFDoc_ShapeTool::GetSubShapes ( L, seq );
1206 Standard_Boolean isVisible = Standard_True;
1207 for ( Standard_Integer j = 1; j <= seq.Length(); j++ ) {
1208 TDF_Label lab = seq.Value(j);
1209 XCAFPrs_Style style;
1210 Quantity_Color C;
574d7236 1211 if ( lab == L ) {
7fd59977 1212 // check for invisible status of object on label
1213 if ( !CTool->IsVisible( lab ) ) {
1214 isVisible = Standard_False;
1215 style.SetVisibility( Standard_False );
1216 }
1217 }
1218 if ( CTool->GetColor ( lab, XCAFDoc_ColorGen, C ) ) {
ec99ba32
G
1219 style.SetColorCurv ( C );
1220 style.SetColorSurf ( C );
7fd59977 1221 }
1222 if ( CTool->GetColor ( lab, XCAFDoc_ColorSurf, C ) )
ec99ba32 1223 style.SetColorSurf ( C );
7fd59977 1224 if ( CTool->GetColor ( lab, XCAFDoc_ColorCurv, C ) )
ec99ba32
G
1225 style.SetColorCurv ( C );
1226
7fd59977 1227 // commented, cause we are need to take reference from
1228// if ( isComponent && lab == L && !isVisible)
1229// if ( !style.IsSetColorSurf() && !style.IsSetColorCurv() ) {
1230// getFatherColor ( L, CTool, style);
1231// }
1232 if ( ! style.IsSetColorCurv() && ! style.IsSetColorSurf() && isVisible ) continue;
1233
1234 TopoDS_Shape sub = XCAFDoc_ShapeTool::GetShape ( lab );
1235 settings.Bind ( sub, style );
1236 }
1237
1238 if ( settings.Extent() <=0 ) continue;
1239
1240 // iterate on subshapes and create STEP styles
1241 Handle(StepVisual_StyledItem) override;
1242 TopTools_MapOfShape Map;
1243
1244 MakeSTEPStyles(Styles,S,settings,override,Map,myMapCompMDGPR,DPDCs,ColRGBs,CTool,0,isComponent);
1245
1246 // create MDGPR and record it in model
1247 Handle(StepVisual_MechanicalDesignGeometricPresentationRepresentation) aMDGPR;
1248
1249 if (!isComponent) {
1250 if ( myMapCompMDGPR.IsBound( aTopSh )) {
0797d9d3 1251#ifdef OCCT_DEBUG
7fd59977 1252 cerr << "Error: Current Top-Level shape have MDGPR already " << endl;
1253#endif
1254 }
1255 Styles.CreateMDGPR ( Context, aMDGPR );
1256 if (!aMDGPR.IsNull())
1257 myMapCompMDGPR.Bind( aTopSh, aMDGPR );
1258 }
1259 else {
1260 // create SDR and add to model.
7f56eba8 1261 const Handle(XSControl_TransferWriter) &TW = WS->TransferWriter();
1262 const Handle(Transfer_FinderProcess) &FP = TW->FinderProcess();
7fd59977 1263 Handle(TransferBRep_ShapeMapper) mapper = TransferBRep::ShapeMapper ( FP, S );
1264 Handle(StepShape_ContextDependentShapeRepresentation) CDSR;
1265 if ( FP->FindTypedTransient(mapper,
1266 STANDARD_TYPE(StepShape_ContextDependentShapeRepresentation),
1267 CDSR) ) {
1268 // create SDR for NAUO
1269 Handle(StepRepr_ProductDefinitionShape) nullPDS; // important to be NULL
1270 Styles.CreateNAUOSRD( Context, CDSR, nullPDS );
1271
1272 // search for MDGPR of the component top-level shape
1273 if ( myMapCompMDGPR.IsBound( aTopSh )) {
1274 aMDGPR = Handle(StepVisual_MechanicalDesignGeometricPresentationRepresentation)::DownCast( myMapCompMDGPR.Find( aTopSh ) );
1275 } else {
1276 aMDGPR = new StepVisual_MechanicalDesignGeometricPresentationRepresentation;
1277 Handle(TCollection_HAsciiString) ReprName = new TCollection_HAsciiString ( "" );
1278 aMDGPR->SetName( ReprName );
1279 aMDGPR->SetContextOfItems( Context );
1280 myMapCompMDGPR.Bind ( aTopSh, aMDGPR );
1281 }
1282 Handle(StepRepr_HArray1OfRepresentationItem) oldItems = aMDGPR->Items();
1283 Standard_Integer oldLengthlen = 0;
1284 if (!oldItems.IsNull())
1285 oldLengthlen = oldItems->Length();
1286 const Standard_Integer nbIt = oldLengthlen + Styles.NbStyles();
1287 if(!nbIt)
1288 continue;
1289 Handle(StepRepr_HArray1OfRepresentationItem) newItems =
1290 new StepRepr_HArray1OfRepresentationItem(1, nbIt);
1291 Standard_Integer si;
1292 Standard_Integer el = 1;
1293 for ( si=1; si <= oldLengthlen; si++ )
1294 newItems->SetValue( el++, oldItems->Value( si ) );
1295 for ( si=1; si <= Styles.NbStyles(); si++ ) {
a9dde4a3 1296 newItems->SetValue( el++, Styles.Style(si));
7fd59977 1297 }
574d7236 1298
7fd59977 1299 if (newItems->Length() > 0)
1300 aMDGPR->SetItems( newItems );
1301 } //end of work with CDSR
1302 }
574d7236 1303 if ( !isVisible ) {
1304 // create invisibility item and refer for stiledItem
1305 Handle(StepVisual_Invisibility) Invsblt = new StepVisual_Invisibility();
1306 Handle(StepVisual_HArray1OfInvisibleItem) HInvsblItm =
1307 new StepVisual_HArray1OfInvisibleItem (1,Styles.NbStyles());
1308 // put all style item into the harray
1309 for ( Standard_Integer si=1; si <= Styles.NbStyles(); si++ ) {
a9dde4a3 1310 Handle(StepRepr_RepresentationItem) styledItm = Styles.Style(si);
574d7236 1311 StepVisual_InvisibleItem anInvItem;
1312 anInvItem.SetValue( styledItm );
1313 HInvsblItm->SetValue( si, anInvItem );
1314 }
1315 // set the invisibility of items
1316 Invsblt->Init( HInvsblItm );
1317 WS->Model()->AddWithRefs( Invsblt );
1318 }
7fd59977 1319 }
1320
1321 return Standard_True;
1322}
1323
1324
1325//=======================================================================
1326//function : WriteNames
1327//purpose :
1328//=======================================================================
1329
1330Standard_Boolean STEPCAFControl_Writer::WriteNames (const Handle(XSControl_WorkSession) &WS,
1331 const TDF_LabelSequence &labels) const
1332{
1333 if ( labels.Length() <=0 ) return Standard_False;
1334
1335 // get working data
7f56eba8 1336 const Handle(XSControl_TransferWriter) &TW = WS->TransferWriter();
1337 const Handle(Transfer_FinderProcess) &FP = TW->FinderProcess();
7fd59977 1338
1339 // Iterate on requested shapes
1340 for ( Standard_Integer i=1; i <= labels.Length(); i++ ) {
1341 TDF_Label L = labels.Value(i);
1342
1343 // get name
1344 Handle(TCollection_HAsciiString) hName = new TCollection_HAsciiString;
1345 if ( ! GetLabelName (L, hName) ) continue;
1346// Handle(TDataStd_Name) N;
1347// if ( ! L.FindAttribute ( TDataStd_Name::GetID(), N ) ) continue;
1348// TCollection_ExtendedString name = N->Get();
1349// if ( name.Length() <=0 ) continue;
1350
1351 // find target STEP entity for the current shape
1352// TopoDS_Shape S;
1353// if ( ! XCAFDoc_ShapeTool::GetShape ( L, S ) ) continue;
1354 if ( ! myLabels.IsBound ( L ) ) continue; // not recorded as translated, skip
1355 TopoDS_Shape S = myLabels.Find ( L );
1356
1357 Handle(StepShape_ShapeDefinitionRepresentation) SDR;
1358 Handle(TransferBRep_ShapeMapper) mapper = TransferBRep::ShapeMapper ( FP, S );
1359 if ( ! FP->FindTypedTransient ( mapper, STANDARD_TYPE(StepShape_ShapeDefinitionRepresentation), SDR ) ) {
0797d9d3 1360#ifdef OCCT_DEBUG
7fd59977 1361 cout << "Warning: Cannot find SDR for " << S.TShape()->DynamicType()->Name() << endl;
1362#endif
1363 continue;
1364 }
1365
1366 // set the name to the PRODUCT
1367 Handle(StepRepr_PropertyDefinition) PropD = SDR->Definition().PropertyDefinition();
1368 if ( PropD.IsNull() ) continue;
1369 Handle(StepBasic_ProductDefinition) PD = PropD->Definition().ProductDefinition();
1370 if ( PD.IsNull() ) continue;
1371 Handle(StepBasic_Product) Prod = PD->Formation()->OfProduct();
1372
1373 Prod->SetId ( hName );
1374 Prod->SetName ( hName );
1375
1376 // write names for components of assemblies
1377 if ( XCAFDoc_ShapeTool::IsAssembly ( L ) ) {
1378 TDF_LabelSequence seq;
1379 XCAFDoc_ShapeTool::GetComponents ( L, seq );
1380 for (Standard_Integer k=1; k <= seq.Length(); k++) {
1381 TDF_Label lab = seq(k);
1382
1383 // get shape with correct location
1384 TDF_Label Lref;
1385 if ( ! XCAFDoc_ShapeTool::GetReferredShape ( lab, Lref ) ||
1386 ! myLabels.IsBound ( Lref ) ) continue;
1387 S = myLabels.Find ( Lref );
1388 S.Move ( XCAFDoc_ShapeTool::GetLocation ( lab ) );
1389
1390 hName = new TCollection_HAsciiString;
1391 if ( ! GetLabelName (lab, hName) ) continue;
1392
1393 // find the target CDSR corresponding to a shape
1394 mapper = TransferBRep::ShapeMapper ( FP, S );
1395 Handle(Transfer_Binder) binder = FP->Find ( mapper );
1396 Handle(StepShape_ContextDependentShapeRepresentation) CDSR;
1397 if ( ! FP->FindTypedTransient (mapper,STANDARD_TYPE(StepShape_ContextDependentShapeRepresentation), CDSR) )
1398 continue;
1399 Handle(StepRepr_ProductDefinitionShape) PDS = CDSR->RepresentedProductRelation();
1400 Handle(StepBasic_ProductDefinitionRelationship) NAUO = PDS->Definition().ProductDefinitionRelationship();
1401 if ( ! NAUO.IsNull() ) NAUO->SetName ( hName );
1402 }
1403 }
1404 }
1405
1406 return Standard_True;
1407}
1408
1409
1410//=======================================================================
1411//function : WritePropsForLabel
1412//purpose :
1413//=======================================================================
1414static Standard_Boolean WritePropsForLabel(const Handle(XSControl_WorkSession) &WS,
1415 const Handle(XCAFDoc_ShapeTool) &aSTool,
1416 const STEPCAFControl_DataMapOfLabelShape &myLabels,
1417 const TDF_Label &L,
1418 const Standard_CString multi)
1419{
1420 if(L.IsNull()) return Standard_False;
1421
1422 STEPConstruct_ValidationProps Props ( WS );
1423
1424 TopoDS_Shape S = aSTool->GetShape(L);
1425 if(S.IsNull()) return Standard_False;
1426
1427 if ( ! multi || XCAFDoc_ShapeTool::IsAssembly ( L ) ) {
1428 // write area
1429 Handle(XCAFDoc_Area) A;
1430 L.FindAttribute ( XCAFDoc_Area::GetID(), A );
1431 if ( ! A.IsNull() ) {
1432 Props.AddArea ( S, A->Get() );
1433 }
1434 // write volume
1435 Handle(XCAFDoc_Volume) V;
1436 L.FindAttribute ( XCAFDoc_Volume::GetID(), V );
1437 if ( ! V.IsNull() ) {
1438 Props.AddVolume ( S, V->Get() );
1439 }
1440 }
1441 // write centroid
1442 Handle(XCAFDoc_Centroid) C;
1443 L.FindAttribute ( XCAFDoc_Centroid::GetID(), C );
1444 if ( ! C.IsNull() ) {
1445 Props.AddCentroid ( S, C->Get() );
1446 }
1447
1448 if( XCAFDoc_ShapeTool::IsCompound(L) || XCAFDoc_ShapeTool::IsAssembly(L) ) {
1449 if(L.HasChild()) {
1450 for(Standard_Integer ich=1; ich<=L.NbChildren(); ich++) {
1451 WritePropsForLabel(WS,aSTool,myLabels,L.FindChild(ich),multi);
1452 }
1453 }
1454 }
1455
1456 return Standard_True;
1457}
1458
1459
1460//=======================================================================
1461//function : WriteValProps
1462//purpose :
1463//=======================================================================
1464
1465Standard_Boolean STEPCAFControl_Writer::WriteValProps (const Handle(XSControl_WorkSession) &WS,
1466 const TDF_LabelSequence &labels,
1467 const Standard_CString multi) const
1468{
1469 if ( labels.Length() <=0 ) return Standard_False;
1470
1471 // get working data
1472// STEPConstruct_ValidationProps Props ( WS );
1473 Handle(XCAFDoc_ShapeTool) aSTool = XCAFDoc_DocumentTool::ShapeTool( labels(1) );
1474
1475 // Iterate on requested shapes
1476 for ( Standard_Integer i=1; i <= labels.Length(); i++ ) {
1477 TDF_Label L = labels.Value(i);
1478
1479 WritePropsForLabel(WS,aSTool,myLabels,L,multi);
1480/*
1481 // find target STEP entity for the current shape
1482 if ( ! myLabels.IsBound ( L ) ) continue; // not recorded as translated, skip
1483 TopoDS_Shape S = myLabels.Find ( L );
1484
1485 // write area and volume (except for components in multifile mode)
1486 if ( ! multi || XCAFDoc_ShapeTool::IsAssembly ( L ) ) {
1487 Handle(XCAFDoc_Area) A;
1488 L.FindAttribute ( XCAFDoc_Area::GetID(), A );
1489 if ( ! A.IsNull() ) Props.AddArea ( S, A->Get() );
1490
1491 Handle(XCAFDoc_Volume) V;
1492 L.FindAttribute ( XCAFDoc_Volume::GetID(), V );
1493 if ( ! V.IsNull() ) Props.AddVolume ( S, V->Get() );
1494 }
1495
1496 // write centroid
1497 Handle(XCAFDoc_Centroid) C;
1498 L.FindAttribute ( XCAFDoc_Centroid::GetID(), C );
1499 if ( ! C.IsNull() ) Props.AddCentroid ( S, C->Get() );
1500
1501 // write centroid for components of assemblies
1502 if ( XCAFDoc_ShapeTool::IsAssembly ( L ) ) {
1503 TDF_LabelSequence seq;
1504 XCAFDoc_ShapeTool::GetComponents ( L, seq );
1505 for (Standard_Integer k=1; k <= seq.Length(); k++) {
1506 TDF_Label lab = seq(k);
1507
1508 // get shape with correct location
1509 TDF_Label Lref;
1510 if ( ! XCAFDoc_ShapeTool::GetReferredShape ( lab, Lref ) ||
1511 ! myLabels.IsBound ( Lref ) ) continue;
1512 TopLoc_Location Loc = XCAFDoc_ShapeTool::GetLocation ( lab );
1513 S = myLabels.Find ( Lref );
1514 S.Move ( Loc );
1515
1516 C.Nullify();
1517 lab.FindAttribute ( XCAFDoc_Centroid::GetID(), C );
1518 // if centroid is not assigned to an instance,
1519 // use (shifted) centroid of original shape
1520 gp_Pnt center;
1521 if ( C.IsNull() ) {
1522 Lref.FindAttribute ( XCAFDoc_Centroid::GetID(), C );
1523 if ( C.IsNull() ) continue;
1524 center = C->Get().Transformed ( Loc.Transformation() );
1525 }
1526 else center = C->Get();
1527
1528 Props.AddCentroid ( S, center, Standard_True );
1529 }
1530 }
1531*/
1532 }
1533
1534 return Standard_True;
1535}
1536
1537
1538//=======================================================================
1539//function : WriteLayers
1540//purpose :
1541//=======================================================================
1542
1543Standard_Boolean STEPCAFControl_Writer::WriteLayers (const Handle(XSControl_WorkSession) &WS,
1544 const TDF_LabelSequence &labels ) const
1545{
1546
1547 if ( labels.Length() <=0 ) return Standard_False;
1548
1549 // get working data
7f56eba8 1550 const Handle(Interface_InterfaceModel) &Model = WS->Model();
1551 const Handle(XSControl_TransferWriter) &TW = WS->TransferWriter();
1552 const Handle(Transfer_FinderProcess) &FP = TW->FinderProcess();
7fd59977 1553 Handle(XCAFDoc_LayerTool) LTool = XCAFDoc_DocumentTool::LayerTool( labels(1) );
1554 if (LTool.IsNull() ) return Standard_False;
1555
1556 TDF_LabelSequence LayerLS;
1557 LTool->GetLayerLabels(LayerLS);
1558 if ( LayerLS.Length() <=0 ) return Standard_False;
1559
1560 // Iterate on requested layers and for each layer take set of shapes.
1561 for ( Standard_Integer i=1; i <= LayerLS.Length(); i++ ) {
1562 TDF_Label L = LayerLS.Value(i);
1563
1564 // get labels of shapes in that layer
1565 TDF_LabelSequence ShapeLs;
1566 LTool->GetShapesOfLayer(L, ShapeLs);
1567 if ( ShapeLs.Length() <=0 ) continue;
1568
1569 // name of layer: if not set, is considered as being empty
1570 Handle(TCollection_HAsciiString) hName = new TCollection_HAsciiString;
1571 GetLabelName ( L, hName );
1572
1573 // Find target STEP entity for each shape and add to StepVisual_PresentationLayerAssignment items.
1574 TColStd_SequenceOfTransient seqRI;
1575 for ( Standard_Integer j=1; j <= ShapeLs.Length(); j++) {
1576 TDF_Label shLabel = ShapeLs.Value(j);
1577 if ( shLabel.IsNull() ) continue;
1578
1579 // there is no way to assign layer to instance in STEP
1580 if ( XCAFDoc_ShapeTool::IsAssembly ( shLabel ) ||
1581 XCAFDoc_ShapeTool::IsReference ( shLabel ) )
1582 continue;
1583
1584 // check that the shape is one of (uub)labels written during current transfer
1585 Standard_Integer k = 1;
1586 for ( ; k <= labels.Length(); k++ )
1587 if ( shLabel.IsDescendant ( labels(k) ) ) break;
1588 if ( k > labels.Length() ) continue;
1589
1590 // get target STEP entity
1591 TopoDS_Shape oneShape = XCAFDoc_ShapeTool::GetShape(shLabel);
1592
1593 TopLoc_Location Loc;
1594 Standard_Integer nb =
1595 FindEntities ( FP, oneShape, Loc, seqRI );
1596 if ( nb <=0 )
1597 FP->Messenger() << "Warning: Cannot find RI for " << oneShape.TShape()->DynamicType()->Name() << endl;
1598 }
1599 if ( seqRI.Length() <= 0 ) continue;
1600
1601 // analyze visibility
1602 Handle(StepVisual_PresentationLayerAssignment) StepLayerAs = new StepVisual_PresentationLayerAssignment;
1603 Handle(TCollection_HAsciiString) descr;
1604 Handle(TDataStd_UAttribute) aUAttr;
1605 Standard_Boolean isLinv = Standard_False;
1606 if (L.FindAttribute(XCAFDoc::InvisibleGUID(), aUAttr)) {
1607 descr = new TCollection_HAsciiString ("invisible");
0797d9d3 1608#ifdef OCCT_DEBUG
7fd59977 1609 FP->Messenger() << "\tLayer \"" << hName->String().ToCString() << "\" is invisible"<<endl;
1610#endif
1611 isLinv = Standard_True;
1612 }
1613 else descr = new TCollection_HAsciiString ("visible");
1614
1615 // create layer entity
1616 Handle(StepVisual_HArray1OfLayeredItem) HArrayOfLItem =
1617 new StepVisual_HArray1OfLayeredItem ( 1, seqRI.Length() );
1618 for (Standard_Integer i1 = 1; i1<=seqRI.Length(); i1++) {
1619 StepVisual_LayeredItem LI;
1620 LI.SetValue ( seqRI.Value(i1) );
1621 HArrayOfLItem->SetValue( i1, LI );
1622 }
1623 StepLayerAs->Init(hName, descr, HArrayOfLItem);
1624 Model->AddWithRefs( StepLayerAs );
1625 // PTV 23.01.2003 add the invisibility AFTER adding layer into the model.
1626 // add the invisibility for the layer
1627 if (isLinv) {
1628 // Invisibility Item for containig invisible layers.
1629 Handle(StepVisual_HArray1OfInvisibleItem) HInvsblItm = new StepVisual_HArray1OfInvisibleItem (1,1);
1630 StepVisual_InvisibleItem InvIt;
1631 InvIt.SetValue( StepLayerAs );
1632 HInvsblItm->SetValue( 1, InvIt);
1633
1634 Handle(StepVisual_Invisibility) Invsblt = new StepVisual_Invisibility();
1635 Invsblt->Init( HInvsblItm );
1636 Model->AddWithRefs( Invsblt );
1637 }
1638 }
1639 return Standard_True;
1640}
1641
1642
1643//=======================================================================
1644//function : getSHUOstyle
1645//purpose : auxilary
1646//=======================================================================
1647static Standard_Boolean getSHUOstyle(const TDF_Label& aSHUOlab,
1648 const Handle(XCAFDoc_ColorTool)& CTool,
1649 XCAFPrs_Style& SHUOstyle)
1650{
1651 Quantity_Color C;
1652 if (!CTool->IsVisible( aSHUOlab ) )
1653 SHUOstyle.SetVisibility(Standard_False);
1654 else {
1655 if ( CTool->GetColor ( aSHUOlab, XCAFDoc_ColorGen, C ) ) {
1656 SHUOstyle.SetColorCurv ( C );
1657 SHUOstyle.SetColorSurf ( C );
1658 }
1659 if ( CTool->GetColor ( aSHUOlab, XCAFDoc_ColorSurf, C ) )
1660 SHUOstyle.SetColorSurf ( C );
1661 if ( CTool->GetColor ( aSHUOlab, XCAFDoc_ColorCurv, C ) )
1662 SHUOstyle.SetColorCurv ( C );
1663 }
1664 if ( !SHUOstyle.IsSetColorCurv() &&
1665 !SHUOstyle.IsSetColorSurf() &&
1666 SHUOstyle.IsVisible() )
1667 return Standard_False;
1668 return Standard_True;
1669}
1670
1671
1672//=======================================================================
1673//function : getProDefinitionOfNAUO
1674//purpose : auxilary
1675//=======================================================================
1676static Standard_Boolean getProDefinitionOfNAUO(const Handle(XSControl_WorkSession)& WS,
1677 const TopoDS_Shape& theShape,
1678 Handle(StepBasic_ProductDefinition)& PD,
1679 Handle(StepRepr_NextAssemblyUsageOccurrence)& NAUO,
1680 Standard_Boolean IsRelating)
1681{
1682 if ( theShape.IsNull() )
1683 return Standard_False;
1684 // get CDSR
7f56eba8 1685 const Handle(XSControl_TransferWriter) &TW = WS->TransferWriter();
1686 const Handle(Transfer_FinderProcess) &FP = TW->FinderProcess();
7fd59977 1687 Handle(StepShape_ContextDependentShapeRepresentation) CDSR;
1688 Handle(TransferBRep_ShapeMapper) mapper = TransferBRep::ShapeMapper ( FP, theShape );
1689 if (!FP->FindTypedTransient(mapper,
1690 STANDARD_TYPE(StepShape_ContextDependentShapeRepresentation),
1691 CDSR))
1692 return Standard_False;
1693 // get PDS of NAUO
1694 Handle(StepRepr_ProductDefinitionShape) PDS = CDSR->RepresentedProductRelation();
1695 if (PDS.IsNull())
1696 return Standard_False;
1697 // get the NAUO entity
1698 Interface_Graph aGraph = WS->HGraph()->Graph();
1699 Interface_EntityIterator subs = aGraph.Shareds(PDS);
1700 for ( subs.Start(); subs.More(); subs.Next() ) {
1701 if (!subs.Value()->IsKind(STANDARD_TYPE(StepRepr_NextAssemblyUsageOccurrence)))
1702 continue;
1703 NAUO = Handle(StepRepr_NextAssemblyUsageOccurrence)::DownCast(subs.Value());
1704 break;
1705 }
1706 if ( NAUO.IsNull() )
1707 return Standard_False;
1708 // get Relatinf or Related product definition
1709 if ( !IsRelating )
1710 PD = NAUO->RelatedProductDefinition();
1711 else
1712 PD = NAUO->RelatingProductDefinition();
1713 if ( PD.IsNull() )
1714 return Standard_False;
1715 return Standard_True;
1716}
1717
1718
1719//=======================================================================
1720//function : writeSHUO
1721//purpose : auxilary
1722//=======================================================================
1723static Standard_Boolean writeSHUO (const Handle(XCAFDoc_GraphNode)& theSHUO,
1724 const Handle(XCAFDoc_ShapeTool)& theSTool,
1725 const Handle(XSControl_WorkSession)& WS,
1726 Handle(StepRepr_SpecifiedHigherUsageOccurrence)& theTopSHUO,
1727 TopoDS_Shape& NAUOShape,
1728 Handle(StepBasic_ProductDefinition)& theRelatingPD,
1729 Standard_Boolean& isDeepest)
1730{
1731 // set the ProductDefinitionRelationship descriptin information as empty strings.
1732 Handle(TCollection_HAsciiString) EmptyString = new TCollection_HAsciiString("");
1733
1734 TDF_LabelSequence aNextUsageLabs;
1735 theSTool->GetSHUONextUsage( theSHUO->Label(), aNextUsageLabs );
1736 Handle(XCAFDoc_GraphNode) NuSHUO;
1737 if ( theTopSHUO.IsNull() ) {
1738 // the top SHUO
1739 if (aNextUsageLabs.Length() < 1)
1740 return Standard_False;
1741 theSTool->GetSHUO( aNextUsageLabs.Value(1), NuSHUO );
1742 if (NuSHUO.IsNull())
1743 return Standard_False;
1744 // get relating product definition
1745 TopoDS_Shape aTopCompShape = theSTool->GetShape( theSHUO->Label().Father() );
1746 Handle(StepRepr_NextAssemblyUsageOccurrence) UUNAUO;
1747 if (!getProDefinitionOfNAUO( WS, aTopCompShape,
1748 theRelatingPD, UUNAUO, Standard_True ))
1749 return Standard_False;
1750 // get related product definition
1751 TopoDS_Shape aNUShape = theSTool->GetShape( NuSHUO->Label().Father() );
1752 Handle(StepBasic_ProductDefinition) aRelatedPD;
1753 Handle(StepRepr_NextAssemblyUsageOccurrence) NUNAUO;
1754 if (!getProDefinitionOfNAUO( WS, aNUShape,
1755 aRelatedPD, NUNAUO, Standard_False ))
1756 return Standard_False;
1757
1758 theTopSHUO = new StepRepr_SpecifiedHigherUsageOccurrence;
1759 // create deepest shuo EmptyString
1760 theTopSHUO->Init(/*id*/EmptyString, /*name*/EmptyString,
1761 /*no description*/Standard_False,/*description*/EmptyString,
1762 theRelatingPD, aRelatedPD,
1763 /*no ACURefDesignator*/Standard_False,/*ACURefDesignator*/EmptyString,
1764 /*upper_usage*/UUNAUO, /*next_usage*/NUNAUO);
1765 // write the other SHUO.
1766 if(!writeSHUO( NuSHUO, theSTool, WS, theTopSHUO, NAUOShape, theRelatingPD, isDeepest )) {
1767 theTopSHUO.Nullify();
1768 return Standard_False;
1769 }
1770
1771 return Standard_True;
1772 }
1773// Handle(XCAFDoc_GraphNode) NuSHUO;
1774 if ( aNextUsageLabs.Length() > 0) {
1775 // store SHUO recursive
0797d9d3 1776#ifdef OCCT_DEBUG
7fd59977 1777 if ( aNextUsageLabs.Length() > 1 )
1778 cout << "Warning: store only one next_usage of current SHUO" << endl;
1779#endif
1780 theSTool->GetSHUO( aNextUsageLabs.Value(1), NuSHUO );
1781 Handle(StepRepr_SpecifiedHigherUsageOccurrence) aNUEntSHUO =
1782 new StepRepr_SpecifiedHigherUsageOccurrence;
1783 if (!writeSHUO( NuSHUO, theSTool, WS, aNUEntSHUO, NAUOShape, theRelatingPD, isDeepest ))
1784 return Standard_False;
1785
1786 // store the deepest SHUO to the dociment
1787 TopoDS_Shape aNUSh, aUUSh;
1788 aNUSh = theSTool->GetShape( NuSHUO->Label().Father() );
1789 aUUSh = theSTool->GetShape( theSHUO->Label().Father() );
1790 // get relating PD with upper_usage and related PD with next_usage
1791 Handle(StepBasic_ProductDefinition) nullPD;// no need to use,case have shared <theRelatingPD>
1792 Handle(StepBasic_ProductDefinition) aRelatedPD;
1793 Handle(StepRepr_NextAssemblyUsageOccurrence) UUNAUO, NUNAUO;
1794 if (!getProDefinitionOfNAUO( WS, aUUSh, nullPD, UUNAUO, Standard_True ) ||
1795 !getProDefinitionOfNAUO( WS, aNUSh, aRelatedPD, NUNAUO, Standard_False )) {
0797d9d3 1796#ifdef OCCT_DEBUG
7fd59977 1797 cout << "Warning: cannot get related or relating PD" << endl;
1798#endif
1799 return Standard_False;
1800 }
1801 aNUEntSHUO->Init(/*id*/EmptyString, /*name*/EmptyString,
1802 /*no description*/Standard_False,/*description*/EmptyString,
1803 theRelatingPD, aRelatedPD,
1804 /*no ACURefDesignator*/Standard_False,/*ACURefDesignator*/EmptyString,
1805 /*upper_usage*/theTopSHUO, /*next_usage*/NUNAUO);
1806 if ( isDeepest ) {
1807 isDeepest = Standard_False;
1808 }
1809 WS->Model()->AddWithRefs ( aNUEntSHUO );
1810 return Standard_True;
1811 } // end of recurse storing
1812
1813 // get shape
1814 TDF_Label aShapeL = theSHUO->Label().Father();
1815 NAUOShape = theSTool->GetShape( aShapeL );
1816 // return to the deepest level from SHUO shape level
1817 // it is because SHUO is attribute on deep level and shape level.
1818 isDeepest = Standard_True;
1819 return Standard_True;
1820}
1821
1822
1823//=======================================================================
1824//function : createSHUOStyledItem
1825//purpose : auxilary
1826//=======================================================================
1827static Standard_Boolean createSHUOStyledItem (const XCAFPrs_Style& style,
1828 const Handle(StepRepr_ProductDefinitionShape)& PDS,
1829 const Handle(XSControl_WorkSession) &WS,
1830 const TopoDS_Shape& Sh,
1831 const Handle(XCAFDoc_ShapeTool)& STool,
1832 MoniTool_DataMapOfShapeTransient& myMapCompMDGPR)
1833{
1834 // create styled item for the indicated SHUO and store to the model
1835 STEPConstruct_Styles Styles( WS );
1836 // translate colors to STEP
1837 Handle(StepVisual_Colour) surfColor, curvColor;
1838 if ( style.IsSetColorSurf() )
1839 surfColor = Styles.EncodeColor ( style.GetColorSurf() );
1840 if ( style.IsSetColorCurv() )
1841 curvColor = Styles.EncodeColor ( style.GetColorCurv() );
1842 Standard_Boolean isComponent = Standard_True;// cause need to get PSBC
1843 Handle(StepRepr_RepresentationItem) item;
1844 // set default color for invisible SHUO.
1845 Standard_Boolean isSetDefaultColor = Standard_False;
1846 if (surfColor.IsNull() && curvColor.IsNull() && !style.IsVisible() ) {
1847 surfColor = Styles.EncodeColor ( Quantity_Color( 1, 1, 1, Quantity_TOC_RGB ) );
1848 isSetDefaultColor = Standard_True;
1849 }
1850 Handle(StepVisual_PresentationStyleAssignment) PSA =
1851 Styles.MakeColorPSA ( item, surfColor, curvColor, isComponent );
1852 Handle(StepVisual_StyledItem) override; //null styled item
1853
1854 // find the repr item of the shape
7f56eba8 1855 const Handle(XSControl_TransferWriter) &TW = WS->TransferWriter();
1856 const Handle(Transfer_FinderProcess) &FP = TW->FinderProcess();
7fd59977 1857 Handle(TransferBRep_ShapeMapper) mapper = TransferBRep::ShapeMapper ( FP, Sh );
1858 Handle(StepShape_ContextDependentShapeRepresentation) CDSR;
1859 FP->FindTypedTransient(mapper,
1860 STANDARD_TYPE(StepShape_ContextDependentShapeRepresentation),
1861 CDSR);
1862 if ( CDSR.IsNull() )
1863 return Standard_False;
1864 // find context
1865 Handle(StepRepr_RepresentationContext) Context = Styles.FindContext( Sh );
1866 TopoDS_Shape aTopSh = Sh;
1867 if (Context.IsNull()) {
1868 TDF_Label aTopShL = STool->FindShape(Sh, Standard_False);
1869 if (aTopShL.IsNull())
1870 return Standard_False;
1871 aTopSh = STool->GetShape( aTopShL );
1872 Context = Styles.FindContext ( aTopSh );
1873 }
1874 if (Context.IsNull())
1875 return Standard_False;
1876 // get representation item of the shape
1877 TopLoc_Location L;
1878 TColStd_SequenceOfTransient seqRI;
1879 FindEntities ( FP, Sh, L, seqRI );
0797d9d3 1880#ifdef OCCT_DEBUG
7fd59977 1881 if ( seqRI.Length() <=0 )
1882 FP->Messenger() << "Warning: Cannot find RI for " << Sh.TShape()->DynamicType()->Name() << endl;
1883#endif
1884 item = Handle(StepRepr_RepresentationItem)::DownCast(seqRI(1));
1885 //get overridden styled item
1886 getStyledItem(Sh,STool, Styles, override,myMapCompMDGPR);
1887
1888 // get STEP STYLED ITEM
1889 Handle(StepVisual_StyledItem) STEPstyle = Styles.AddStyle ( item, PSA, override );
1890 // create SR, SDR and all necessary references between them and ST, PDS, PSBC, GRC
1891 Styles.CreateNAUOSRD( Context, CDSR, PDS );
1892
1893 // add step styled item of SHUO to the model
1894 // do it by additing styled item to the MDGPR
1895 if ( !aTopSh.IsNull() && !myMapCompMDGPR.IsBound( aTopSh ) ) {
1896 // create MDGPR and record it in model
0797d9d3 1897#ifdef OCCT_DEBUG
7fd59977 1898 cout << "Warning: " << __FILE__ << ": Create new MDGPR for SHUO instance" << endl;
1899#endif
1900 Handle(StepVisual_MechanicalDesignGeometricPresentationRepresentation) aMDGPR;
1901 Styles.CreateMDGPR ( Context, aMDGPR );
1902 if (!aMDGPR.IsNull())
1903 myMapCompMDGPR.Bind( aTopSh, aMDGPR );
1904 }
1905 else if ( !aTopSh.IsNull() && myMapCompMDGPR.IsBound( aTopSh ) ) {
1906 // get MDGPR of the top-level shape
1907 Handle(StepVisual_PresentationRepresentation) aMDGPR =
1908 Handle(StepVisual_PresentationRepresentation)::DownCast( myMapCompMDGPR.Find( aTopSh ) );
1909 // get old styled items to not lose it
1910 Handle(StepRepr_HArray1OfRepresentationItem) oldItems = aMDGPR->Items();
1911 Standard_Integer oldLengthlen = 0;
1912 if (!oldItems.IsNull())
1913 oldLengthlen = oldItems->Length();
1914 // create new array of styled items by an olds and new one
1915 Handle(StepRepr_HArray1OfRepresentationItem) newItems =
1916 new StepRepr_HArray1OfRepresentationItem(1, oldLengthlen + 1);
1917 Standard_Integer si;
1918 Standard_Integer el = 1;
1919 for ( si=1; si <= oldLengthlen; si++ )
1920 newItems->SetValue( el++, oldItems->Value( si ) );
a9dde4a3 1921 newItems->SetValue (el++, STEPstyle);
7fd59977 1922 // init MDGPR be new array of styled items
1923 if (newItems->Length() > 0)
1924 aMDGPR->SetItems( newItems );
1925 }
1926 else {
1927 WS->Model()->AddWithRefs ( STEPstyle ); // add as root to the model, but it is not good
0797d9d3 1928#ifdef OCCT_DEBUG
7fd59977 1929 cout << "Warning: " << __FILE__ << ": adds styled item of SHUO as root, casue cannot find MDGPR" << endl;
1930#endif
1931 }
1932 // create invisibility item for the styled item
1933 if ( !style.IsVisible() ) {
1934 if (isSetDefaultColor) {
1935 // try to set default color from top-level shape
1936
1937 setDefaultInstanceColor(override, PSA);
1938 }
1939 // create invisibility item and refer for stiledItem
1940 Handle(StepVisual_Invisibility) Invsblt = new StepVisual_Invisibility();
1941 Handle(StepVisual_HArray1OfInvisibleItem) HInvsblItm =
1942 new StepVisual_HArray1OfInvisibleItem (1,1);
1943 // put all style item into the harray
1944 StepVisual_InvisibleItem anInvItem;
1945 anInvItem.SetValue( STEPstyle );
1946 HInvsblItm->SetValue( 1, anInvItem );
1947 Invsblt->Init( HInvsblItm );
1948 WS->Model()->AddWithRefs( Invsblt );
1949 }
1950
1951 return Standard_True;
1952}
1953
1954
1955//=======================================================================
1956//function : WriteSHUOs
1957//purpose :
1958//=======================================================================
1959
1960Standard_Boolean STEPCAFControl_Writer::WriteSHUOs (const Handle(XSControl_WorkSession) &WS,
1961 const TDF_LabelSequence &labels )
1962{
1963 if ( labels.Length() <=0 ) return Standard_False;
1964
1965 // get working data
7fd59977 1966 Handle(XCAFDoc_ColorTool) CTool = XCAFDoc_DocumentTool::ColorTool( labels(1) );
1967 if (CTool.IsNull() )
1968 return Standard_False;
1969 // map of transfered SHUO
1970 TColStd_MapOfTransient aMapOfMainSHUO;
7fd59977 1971 // Iterate on requested shapes
1972 for ( Standard_Integer i=1; i <= labels.Length(); i++ ) {
1973 TDF_Label L = labels.Value(i);
1974 if ( ! myLabels.IsBound ( L ) ) continue; // not recorded as translated, skip
7fd59977 1975 if ( XCAFDoc_ShapeTool::IsAssembly ( L ) ) {
1976 TDF_LabelSequence seq;
1977 XCAFDoc_ShapeTool::GetComponents ( L, seq );
1978 // iterates on components of assembly
1979 for (Standard_Integer k=1; k <= seq.Length(); k++) {
1980 TDF_Label lab = seq(k);
1981 TDF_AttributeSequence anAttrSeq;
1982 CTool->ShapeTool()->GetAllComponentSHUO( lab, anAttrSeq );
1983 // work with SHUO
1984 for (Standard_Integer j = 1; j <= anAttrSeq.Length(); j++) {
1985 Handle(XCAFDoc_GraphNode) aSHUO =
1986 Handle(XCAFDoc_GraphNode)::DownCast(anAttrSeq.Value( j ));
1987 // take label of SHUO
1988 TDF_Label aSHUOlab = aSHUO->Label();
1989 TDF_LabelSequence aUpLabels;
1990 // check is it SHUO of upper_usage
1991 CTool->ShapeTool()->GetSHUOUpperUsage( aSHUOlab, aUpLabels );
1992 if ( aUpLabels.Length() > 0 )
1993 continue; // transfer only main SHUO
1994 if ( aMapOfMainSHUO.Contains( aSHUO ) )
1995 continue; // do not try to transfer SHUO twice
1996 aMapOfMainSHUO.Add( aSHUO );
1997 // check if it is styled SHUO
1998 XCAFPrs_Style SHUOstyle;
1999 if ( !getSHUOstyle ( aSHUOlab, CTool, SHUOstyle ) ) {
0797d9d3 2000#ifdef OCCT_DEBUG
7fd59977 2001 cout << "Warning: " << __FILE__ << ": do not store SHUO without any style to the STEP model" << endl;
2002#endif
2003 continue;
2004 }
2005 // write SHUO to the model amd then add structure type.
2006 TopoDS_Shape NAUOShape; // shape of the deepest NAUO in the SHUO structure
2007 Standard_Boolean isDeepest = Standard_False;
2008 Handle(StepRepr_SpecifiedHigherUsageOccurrence) anEntOfSHUO;
2009 Handle(StepBasic_ProductDefinition) aRelatingPD;
2010 // create the top SHUO and all other.
2011 writeSHUO( aSHUO, CTool->ShapeTool(), WS, anEntOfSHUO, NAUOShape, aRelatingPD, isDeepest );
2012 if ( anEntOfSHUO.IsNull() || NAUOShape.IsNull() ) {
0797d9d3 2013#ifdef OCCT_DEBUG
7fd59977 2014 cout << "Warning: " << __FILE__ << ": Cannot store SHUO" << endl;
2015#endif
2016 continue;
2017 }
2018 // create new Product Definition Shape for TOP SHUO
0797d9d3 2019#ifdef OCCT_DEBUG
7fd59977 2020 cout << "Info: " << __FILE__ << ": Create NEW PDS for current SHUO " << endl;
2021#endif
2022 Handle(StepRepr_ProductDefinitionShape) PDS = new StepRepr_ProductDefinitionShape;
2023 Handle(TCollection_HAsciiString) aPDSname = new TCollection_HAsciiString("SHUO");
2024 Handle(TCollection_HAsciiString) descrStr = new TCollection_HAsciiString("");
2025 StepRepr_CharacterizedDefinition aCharDef;
2026 aCharDef.SetValue( anEntOfSHUO );
2027 PDS->Init( aPDSname, Standard_False, descrStr, aCharDef );
2028
2029 // create styled item for SHUO and add to the model
2030 createSHUOStyledItem ( SHUOstyle, PDS, WS, NAUOShape, CTool->ShapeTool(), myMapCompMDGPR );
2031
2032 } // end work with SHUO
2033 } // end of an assembly components
2034 } // end of IsAssembly case
2035 // nothing to do if it is not assembly
2036 continue;
2037 } // end of iterates on indicated labels
2038 return Standard_True;
2039}
2040
2041
2042//=======================================================================
2043//function : FindPDSforDGT
2044//purpose : auxilary: find PDS for AdvancedFace or EdgeCurve for creation
2045// needed ShapeAspect in D&GT structure
2046//=======================================================================
2047static Standard_Boolean FindPDSforDGT(const Interface_Graph &aGraph,
2048 const Handle(Standard_Transient) &ent,
2049 Handle(StepRepr_ProductDefinitionShape) &PDS,
2050 Handle(StepRepr_RepresentationContext) &RC,
2051 Handle(StepShape_AdvancedFace) &AF,
2052 Handle(StepShape_EdgeCurve) &EC)
2053{
2054 if( !ent->IsKind(STANDARD_TYPE(StepShape_EdgeCurve)) &&
2055 !ent->IsKind(STANDARD_TYPE(StepShape_AdvancedFace)) )
2056 return Standard_False;
2057
2058 AF = Handle(StepShape_AdvancedFace)::DownCast(ent);
2059 if( ent->IsKind(STANDARD_TYPE(StepShape_EdgeCurve)) ) {
2060 EC = Handle(StepShape_EdgeCurve)::DownCast(ent);
2061 Interface_EntityIterator subs = aGraph.Sharings(EC);
2062 for(subs.Start(); subs.More() && AF.IsNull(); subs.Next()) {
2063 Handle(StepShape_OrientedEdge) OE = Handle(StepShape_OrientedEdge)::DownCast(subs.Value());
2064 if(OE.IsNull()) continue;
2065 Interface_EntityIterator subs1 = aGraph.Sharings(OE);
2066 for(subs1.Start(); subs1.More() && AF.IsNull(); subs1.Next()) {
2067 Handle(StepShape_EdgeLoop) EL = Handle(StepShape_EdgeLoop)::DownCast(subs1.Value());
2068 if(EL.IsNull()) continue;
2069 Interface_EntityIterator subs2 = aGraph.Sharings(EL);
2070 for(subs2.Start(); subs2.More() && AF.IsNull(); subs2.Next()) {
2071 Handle(StepShape_FaceBound) FB = Handle(StepShape_FaceBound)::DownCast(subs2.Value());
2072 if(FB.IsNull()) continue;
2073 Interface_EntityIterator subs3 = aGraph.Sharings(FB);
2074 for(subs3.Start(); subs3.More() && AF.IsNull(); subs3.Next()) {
2075 AF = Handle(StepShape_AdvancedFace)::DownCast(subs3.Value());
2076 }
2077 }
2078 }
2079 }
2080 }
2081 if(AF.IsNull()) return Standard_False;
2082
2083 Interface_EntityIterator subs = aGraph.Sharings(AF);
2084 for(subs.Start(); subs.More() && PDS.IsNull(); subs.Next()) {
2085 Handle(StepShape_ConnectedFaceSet) CFS =
2086 Handle(StepShape_ConnectedFaceSet)::DownCast(subs.Value());
2087 if(CFS.IsNull()) continue;
2088 Interface_EntityIterator subs1 = aGraph.Sharings(CFS);
2089 for(subs1.Start(); subs1.More() && PDS.IsNull(); subs1.Next()) {
2090 Handle(StepRepr_RepresentationItem) RI =
2091 Handle(StepRepr_RepresentationItem)::DownCast(subs1.Value());
2092 if(RI.IsNull()) continue;
2093 Interface_EntityIterator subs2 = aGraph.Sharings(RI);
2094 for(subs2.Start(); subs2.More() && PDS.IsNull(); subs2.Next()) {
2095 Handle(StepShape_ShapeRepresentation) SR =
2096 Handle(StepShape_ShapeRepresentation)::DownCast(subs2.Value());
2097 if(SR.IsNull()) continue;
2098 RC = SR->ContextOfItems();
2099 Interface_EntityIterator subs3 = aGraph.Sharings(SR);
2100 for(subs3.Start(); subs3.More() && PDS.IsNull(); subs3.Next()) {
2101 Handle(StepShape_ShapeDefinitionRepresentation) SDR =
2102 Handle(StepShape_ShapeDefinitionRepresentation)::DownCast(subs3.Value());
2103 if(SDR.IsNull()) continue;
2104 Handle(StepRepr_PropertyDefinition) PropD = SDR->Definition().PropertyDefinition();
2105 if(PropD.IsNull()) continue;
2106 PDS = Handle(StepRepr_ProductDefinitionShape)::DownCast(PropD);
2107 }
2108 }
2109 }
2110 }
2111
2112 return Standard_True;
2113}
2114
6595eee7 2115//=======================================================================
2116//function : GetUnit
2117//purpose : auxiliary
2118//=======================================================================
2119
2120static StepBasic_Unit GetUnit(const Handle(StepRepr_RepresentationContext)& theRC,
2121 const Standard_Boolean isAngle = Standard_False)
2122{
2123 StepBasic_Unit aUnit;
2124 if (isAngle) {
2125 Handle(StepBasic_SiUnitAndPlaneAngleUnit) aSiPAU;
2126 Handle(StepGeom_GeometricRepresentationContextAndGlobalUnitAssignedContext) aCtx =
2127 Handle(StepGeom_GeometricRepresentationContextAndGlobalUnitAssignedContext)::DownCast(theRC);
2128 if(!aCtx.IsNull()) {
2129 for(Standard_Integer j = 1; j <= aCtx->NbUnits(); j++) {
2130 if(aCtx->UnitsValue(j)->IsKind(STANDARD_TYPE(StepBasic_SiUnitAndPlaneAngleUnit))) {
2131 aSiPAU = Handle(StepBasic_SiUnitAndPlaneAngleUnit)::DownCast(aCtx->UnitsValue(j));
2132 break;
2133 }
2134 }
2135 }
2136 if(aSiPAU.IsNull()) {
2137 Handle(StepGeom_GeomRepContextAndGlobUnitAssCtxAndGlobUncertaintyAssCtx) aCtx1 =
2138 Handle(StepGeom_GeomRepContextAndGlobUnitAssCtxAndGlobUncertaintyAssCtx)::DownCast(theRC);
2139 if(!aCtx1.IsNull()) {
2140 for(Standard_Integer j = 1; j <= aCtx1->NbUnits(); j++) {
2141 if(aCtx1->UnitsValue(j)->IsKind(STANDARD_TYPE(StepBasic_SiUnitAndPlaneAngleUnit))) {
2142 aSiPAU = Handle(StepBasic_SiUnitAndPlaneAngleUnit)::DownCast(aCtx1->UnitsValue(j));
2143 break;
2144 }
2145 }
2146 }
2147 }
2148 if(aSiPAU.IsNull())
2149 aSiPAU = new StepBasic_SiUnitAndPlaneAngleUnit;
2150 aUnit.SetValue(aSiPAU);
2151 }
2152 else {
2153 Handle(StepBasic_SiUnitAndLengthUnit) aSiLU;
2154 Handle(StepGeom_GeometricRepresentationContextAndGlobalUnitAssignedContext) aCtx =
2155 Handle(StepGeom_GeometricRepresentationContextAndGlobalUnitAssignedContext)::DownCast(theRC);
2156 if(!aCtx.IsNull()) {
2157 for(Standard_Integer j = 1; j <= aCtx->NbUnits(); j++) {
2158 if(aCtx->UnitsValue(j)->IsKind(STANDARD_TYPE(StepBasic_SiUnitAndLengthUnit))) {
2159 aSiLU = Handle(StepBasic_SiUnitAndLengthUnit)::DownCast(aCtx->UnitsValue(j));
2160 break;
2161 }
2162 }
2163 }
2164 if(aSiLU.IsNull()) {
2165 Handle(StepGeom_GeomRepContextAndGlobUnitAssCtxAndGlobUncertaintyAssCtx) aCtx1 =
2166 Handle(StepGeom_GeomRepContextAndGlobUnitAssCtxAndGlobUncertaintyAssCtx)::DownCast(theRC);
2167 if(!aCtx1.IsNull()) {
2168 for(Standard_Integer j = 1; j <= aCtx1->NbUnits(); j++) {
2169 if(aCtx1->UnitsValue(j)->IsKind(STANDARD_TYPE(StepBasic_SiUnitAndLengthUnit))) {
2170 aSiLU = Handle(StepBasic_SiUnitAndLengthUnit)::DownCast(aCtx1->UnitsValue(j));
2171 break;
2172 }
2173 }
2174 }
2175 }
2176 if(aSiLU.IsNull())
2177 aSiLU = new StepBasic_SiUnitAndLengthUnit;
2178 aUnit.SetValue(aSiLU);
2179 }
2180 return aUnit;
2181}
2182
2183//=======================================================================
2184//function : CreateDimValue
2185//purpose : auxiliary
2186//======================================================================
2187static Handle(StepRepr_ReprItemAndMeasureWithUnit) CreateDimValue(const Standard_Real theValue,
2188 const StepBasic_Unit theUnit,
2189 const Handle(TCollection_HAsciiString)& theName,
2190 const Standard_CString theMeasureName,
2191 const Standard_Boolean isAngle,
2192 const Standard_Boolean isQualified = Standard_False,
2193 const Handle(StepShape_QualifiedRepresentationItem)& theQRI = NULL)
2194{
2195 Handle(StepRepr_RepresentationItem) aReprItem = new StepRepr_RepresentationItem();
2196 aReprItem->Init(new TCollection_HAsciiString(theName));
2197 Handle(StepBasic_MeasureWithUnit) aMWU = new StepBasic_MeasureWithUnit();
2198 Handle(StepBasic_MeasureValueMember) aValueMember = new StepBasic_MeasureValueMember();
2199 aValueMember->SetName(theMeasureName);
2200 aValueMember->SetReal(theValue);
2201 aMWU->Init(aValueMember, theUnit);
2202 if (isQualified) {
2203 if (isAngle) {
2204 // Angle & with qualifiers
2205 Handle(StepRepr_ReprItemAndPlaneAngleMeasureWithUnitAndQRI) anItem =
2206 new StepRepr_ReprItemAndPlaneAngleMeasureWithUnitAndQRI();
2207 anItem->Init(aMWU, aReprItem, theQRI);
2208 return anItem;
2209 }
2210 else {
2211 // Length & with qualifiers
2212 Handle(StepRepr_ReprItemAndLengthMeasureWithUnitAndQRI) anItem =
2213 new StepRepr_ReprItemAndLengthMeasureWithUnitAndQRI();
2214 anItem->Init(aMWU, aReprItem, theQRI);
2215 return anItem;
2216 }
2217 }
2218 else {
2219 if (isAngle) {
2220 // Angle & without qualifiers
2221 Handle(StepRepr_ReprItemAndPlaneAngleMeasureWithUnit) anItem =
2222 new StepRepr_ReprItemAndPlaneAngleMeasureWithUnit();
2223 anItem->Init(aMWU, aReprItem);
2224 return anItem;
2225 }
2226 else {
2227 // Length & without qualifiers
2228 Handle(StepRepr_ReprItemAndLengthMeasureWithUnit) anItem =
2229 new StepRepr_ReprItemAndLengthMeasureWithUnit();
2230 anItem->Init(aMWU, aReprItem);
2231 return anItem;
2232 }
2233 }
2234}
2235
2236//=======================================================================
2237//function : WriteShapeAspect
2238//purpose : auxiliary (write Shape_Aspect entity for given shape)
2239//=======================================================================
2240
2241static Handle(StepRepr_ShapeAspect) WriteShapeAspect (const Handle(XSControl_WorkSession) &WS,
2242 const TDF_Label theLabel,
2243 const TopoDS_Shape theShape,
2244 Handle(StepRepr_RepresentationContext)& theRC,
2245 Handle(StepAP242_GeometricItemSpecificUsage)& theGISU)
2246{
2247 // Get working data
7f56eba8 2248 const Handle(Interface_InterfaceModel) &Model = WS->Model();
2249 const Handle(XSControl_TransferWriter) &TW = WS->TransferWriter();
2250 const Handle(Transfer_FinderProcess) &FP = TW->FinderProcess();
6595eee7 2251 const Handle(Interface_HGraph) aHGraph = WS->HGraph();
2252 if (aHGraph.IsNull())
2253 return NULL;
2254 Interface_Graph aGraph = aHGraph->Graph();
2255
2256 TopLoc_Location aLoc;
2257 TColStd_SequenceOfTransient aSeqRI;
2258 FindEntities( FP, theShape, aLoc, aSeqRI );
2259 if ( aSeqRI.Length() <= 0 ) {
2260 FP->Messenger() << "Warning: Cannot find RI for "<<theShape.TShape()->DynamicType()->Name()<<endl;
2261 return NULL;
2262 }
2263
2264 Handle(StepRepr_ProductDefinitionShape) aPDS;
2265 Handle(StepRepr_RepresentationContext) aRC;
2266 Handle(Standard_Transient) anEnt = aSeqRI.Value(1);
2267 Handle(StepShape_AdvancedFace) anAF;
2268 Handle(StepShape_EdgeCurve) anEC;
2269 FindPDSforDGT(aGraph, anEnt, aPDS, aRC, anAF, anEC);
2270 if(aPDS.IsNull())
2271 return NULL;
2272
2273 theRC = aRC;
2274 // Shape_Aspect
2275 Handle(TCollection_HAsciiString) aName = new TCollection_HAsciiString();
2276 Handle(TDataStd_Name) aNameAttr;
2277 if (theLabel.FindAttribute(TDataStd_Name::GetID(), aNameAttr)) {
2278 aName = new TCollection_HAsciiString(TCollection_AsciiString(aNameAttr->Get(), '?'));
2279 Standard_Integer aFirstSpace = aName->Search(" ");
2280 if (aFirstSpace != -1)
2281 aName = aName->SubString(aFirstSpace + 1, aName->Length());
2282 else
2283 aName = new TCollection_HAsciiString();
2284 }
2285 Handle(TCollection_HAsciiString) aDescription = new TCollection_HAsciiString();
2286 Handle(StepRepr_ShapeAspect) aSA = new StepRepr_ShapeAspect;
2287 aSA->Init(aName, aDescription, aPDS, StepData_LTrue);
2288
2289 // Geometric_Item_Specific_Usage
2290 Handle(StepAP242_GeometricItemSpecificUsage) aGISU = new StepAP242_GeometricItemSpecificUsage();
2291 StepAP242_ItemIdentifiedRepresentationUsageDefinition aDefinition;
2292 aDefinition.SetValue(aSA);
2293 Handle(StepRepr_HArray1OfRepresentationItem) anReprItems = new StepRepr_HArray1OfRepresentationItem(1, 1);
2294 Handle(StepRepr_RepresentationItem) anIdentifiedItem = Handle(StepRepr_RepresentationItem)::DownCast(anEnt);
2295 anReprItems->SetValue(1, anIdentifiedItem);
2296 Interface_EntityIterator subs = aGraph.Sharings(aPDS);
2297 Handle(StepShape_ShapeDefinitionRepresentation) aSDR;
2298 for (subs.Start(); subs.More() && aSDR.IsNull(); subs.Next()) {
2299 Handle(Standard_Transient) anEntity = subs.Value();
2300 aSDR = Handle(StepShape_ShapeDefinitionRepresentation)::DownCast(anEntity);
2301 }
2302 if (aSDR.IsNull())
2303 return NULL;
2304
2305 // Set entities to model
2306 aGISU->Init(aName, aDescription, aDefinition, aSDR->UsedRepresentation(), anReprItems);
2307 Model->AddWithRefs(aSA);
2308 Model->AddWithRefs(aGISU);
2309 theGISU = aGISU;
2310 return aSA;
2311}
2312
2313//=======================================================================
b0cef606 2314//function : WritePresentation
2315//purpose : auxiliary (write annotation plane and presentation)
2316//======================================================================
2317static void WritePresentation(const Handle(XSControl_WorkSession) &WS,
2318 const TopoDS_Shape thePresentation,
2319 const gp_Ax2 theAnnotationPlane,
f0bf70e8 2320 const gp_Pnt theTextPosition,
b0cef606 2321 const Handle(Standard_Transient) theDimension)
2322{
2323 if (thePresentation.IsNull())
2324 return;
2325 // Get working data
7f56eba8 2326 const Handle(Interface_InterfaceModel) &aModel = WS->Model();
b0cef606 2327
2328 // Presentation
2329 Handle(StepVisual_TessellatedGeometricSet) aGeomSet = STEPCAFControl_GDTProperty::GetTessellation(thePresentation);
2330 Handle(StepVisual_TessellatedAnnotationOccurrence) aTAO = new StepVisual_TessellatedAnnotationOccurrence();
2331 aTAO->Init(new TCollection_HAsciiString(), gdtPrsCurveStyle, aGeomSet);
2332 StepVisual_DraughtingCalloutElement aDCElement;
2333 aDCElement.SetValue(aTAO);
2334 Handle(StepVisual_HArray1OfDraughtingCalloutElement) aTAOs = new StepVisual_HArray1OfDraughtingCalloutElement(1, 1);
2335 aTAOs->SetValue(1, aDCElement);
2336 Handle(StepVisual_DraughtingCallout) aDCallout = new StepVisual_DraughtingCallout();
2337 aDCallout->Init(new TCollection_HAsciiString(), aTAOs);
2338 Handle(StepRepr_HArray1OfRepresentationItem) aDCsForDMIA = new StepRepr_HArray1OfRepresentationItem(1, 1);
2339 aDCsForDMIA->SetValue(1, aDCallout);
2340 StepAP242_ItemIdentifiedRepresentationUsageDefinition aDimension;
2341 aDimension.SetValue(theDimension);
2342 Handle(StepAP242_DraughtingModelItemAssociation) aDMIA =
2343 new StepAP242_DraughtingModelItemAssociation();
2344 aDMIA->Init(new TCollection_HAsciiString("PMI representation to presentation link"),
2345 new TCollection_HAsciiString(), aDimension, gdtPresentationDM, aDCsForDMIA);
2346 aModel->AddWithRefs(aDMIA);
2347
2348 // Annotation plane
2349 // Presentation Style
2350 Handle(StepVisual_NullStyleMember) aNullStyle = new StepVisual_NullStyleMember();
2351 aNullStyle->SetEnumText(0, ".NULL.");
2352 StepVisual_PresentationStyleSelect aStyleItem;
2353 aStyleItem.SetValue(aNullStyle);
2354 Handle(StepVisual_HArray1OfPresentationStyleSelect) aStyles = new StepVisual_HArray1OfPresentationStyleSelect(1, 1);
2355 aStyles->SetValue(1, aStyleItem);
2356 Handle(StepVisual_PresentationStyleAssignment) aPrsStyle = new StepVisual_PresentationStyleAssignment();
2357 aPrsStyle->Init(aStyles);
2358 Handle(StepVisual_HArray1OfPresentationStyleAssignment) aPrsStyles =
2359 new StepVisual_HArray1OfPresentationStyleAssignment(1, 1);
2360 aPrsStyles->SetValue(1, aPrsStyle);
2361 // Plane
2362 Handle(StepGeom_Plane) aPlane = new StepGeom_Plane();
2363 Handle(StepGeom_Axis2Placement3d) anAxis = STEPCAFControl_GDTProperty::GetAxis2Placement3D(theAnnotationPlane);
f0bf70e8 2364 // Set text position to plane origin
2365 Handle(StepGeom_CartesianPoint) aTextPos = new StepGeom_CartesianPoint();
2366 Handle(TColStd_HArray1OfReal) aCoords = new TColStd_HArray1OfReal(1, 3);
2367 for (Standard_Integer i = 1; i <= 3; i++)
2368 aCoords->SetValue(i, theTextPosition.Coord(i));
2369 aTextPos->Init(new TCollection_HAsciiString(), aCoords);
2370 anAxis->SetLocation(aTextPos);
b0cef606 2371 aPlane->Init(new TCollection_HAsciiString(), anAxis);
2372 // Annotation plane element
2373 StepVisual_AnnotationPlaneElement aPlaneElement;
2374 aPlaneElement.SetValue(aDCallout);
2375 Handle(StepVisual_HArray1OfAnnotationPlaneElement) aDCsForAnnPln = new StepVisual_HArray1OfAnnotationPlaneElement(1, 1);
2376 aDCsForAnnPln->SetValue(1, aPlaneElement);
2377 // Init AnnotationPlane entity
2378 Handle(StepVisual_AnnotationPlane) anAnnPlane = new StepVisual_AnnotationPlane();
2379 anAnnPlane->Init(new TCollection_HAsciiString(), aPrsStyles, aPlane, aDCsForAnnPln);
2380 gdtAnnotationPlanes.Append(anAnnPlane);
2381 aModel->AddWithRefs(anAnnPlane);
2382}
2383
2384//=======================================================================
6595eee7 2385//function : WriteDatumAP242
2386//purpose : auxiliary (write Datum entity for given shape or write all
2387// necessary entities and link them to already written datum
2388// in case of multiple features association)
2389//=======================================================================
2390static Handle(StepDimTol_Datum) WriteDatumAP242(const Handle(XSControl_WorkSession) &WS,
2391 const TDF_Label theShapeL,
2392 const TDF_Label theDatumL,
2393 const Standard_Boolean isFirstDTarget,
2394 const Handle(StepDimTol_Datum) theWrittenDatum)
2395{
2396 // Get working data
7f56eba8 2397 const Handle(Interface_InterfaceModel) &Model = WS->Model();
2398 const Handle(XSControl_TransferWriter) &TW = WS->TransferWriter();
2399 const Handle(Transfer_FinderProcess) &FP = TW->FinderProcess();
6595eee7 2400 const Handle(Interface_HGraph) aHGraph = WS->HGraph();
2401 if (aHGraph.IsNull())
2402 return NULL;
2403 Interface_Graph aGraph = aHGraph->Graph();
2404
2405 Handle(StepRepr_ShapeAspect) aSA;
2406 Handle(StepAP242_GeometricItemSpecificUsage) aGISU;
2407 // Link with datum feature
2408 Handle(StepRepr_ProductDefinitionShape) aPDS;
2409 Handle(StepRepr_RepresentationContext) aRC;
2410 Handle(Standard_Transient) anEnt;
2411 Handle(StepShape_AdvancedFace) anAF;
2412 Handle(StepShape_EdgeCurve) anEC;
2413 TopoDS_Shape aShape;
2414 TopLoc_Location aLoc;
2415 TColStd_SequenceOfTransient aSeqRI;
2416
2417 aShape = XCAFDoc_ShapeTool::GetShape(theShapeL);
2418 FindEntities( FP, aShape, aLoc, aSeqRI );
2419 if ( aSeqRI.Length() <= 0 ) {
2420 FP->Messenger() << "Warning: Cannot find RI for "<<aShape.TShape()->DynamicType()->Name()<<endl;
2421 return NULL;
2422 }
2423 anEnt = aSeqRI.Value(1);
2424 FindPDSforDGT(aGraph, anEnt, aPDS, aRC, anAF, anEC);
2425 if (aPDS.IsNull())
2426 return NULL;
2427
2428 aSA = WriteShapeAspect(WS, theDatumL, aShape, aRC, aGISU);
2429 if (aSA.IsNull())
2430 return NULL;
2431 Standard_Integer aSANum = Model->Number(aSA);
2432 Standard_Integer aGISUNum = Model->Number(aGISU);
2433 // Find if datum has datum targets and get common datum attributes
2434 Handle(XCAFDoc_Datum) aDatumAttr;
2435 if (!theDatumL.FindAttribute(XCAFDoc_Datum::GetID(), aDatumAttr))
2436 return NULL;
2437 Handle(XCAFDimTolObjects_DatumObject) anObject = aDatumAttr->GetObject();
2438 if (anObject.IsNull())
2439 return NULL;
2440 Standard_Boolean isSimpleDatum = !anObject->IsDatumTarget();
2441 Handle(TCollection_HAsciiString) anIdentifier = anObject->GetName();
2442 Handle(TCollection_HAsciiString) aTargetId = (anObject->GetDatumTargetNumber() == 0 ?
2443 new TCollection_HAsciiString() : new TCollection_HAsciiString(anObject->GetDatumTargetNumber()));
2444
2445 // If datum type is area, but there is no area in object, write as simple datum
2446 if (anObject->GetDatumTargetType() == XCAFDimTolObjects_DatumTargetType_Area &&
2447 anObject->GetDatumTarget().IsNull())
2448 isSimpleDatum = Standard_True;
2449
2450 // Simple datum
2451 if (isSimpleDatum) {
2452 Handle(StepDimTol_DatumFeature) aDF = new StepDimTol_DatumFeature();
2453 aDF->Init(new TCollection_HAsciiString(), new TCollection_HAsciiString(), aSA->OfShape(), aSA->ProductDefinitional());
2454 Model->ReplaceEntity(aSANum, aDF);
2455 aSA = aDF;
2456 StepAP242_ItemIdentifiedRepresentationUsageDefinition aDefinition;
2457 aDefinition.SetValue(aDF);
2458 aGISU->SetDefinition(aDefinition);
2459 Model->ReplaceEntity(aGISUNum, aGISU);
2460 }
2461 // Datum with datum targets
2462 else {
2463 XCAFDimTolObjects_DatumTargetType aDatumType = anObject->GetDatumTargetType();
2464 Handle(StepDimTol_DatumTarget) aDatumTarget;
2465 // Note: the given way to write such datum type may be incorrect (too little information)
2466 if (aDatumType == XCAFDimTolObjects_DatumTargetType_Area) {
2467 TopoDS_Shape aDTShape = anObject->GetDatumTarget();
2468 Handle(StepAP242_GeometricItemSpecificUsage) anAreaGISU;
2469 Handle(StepRepr_ShapeAspect) anAreaSA = WriteShapeAspect(WS, theDatumL, aDTShape, aRC, anAreaGISU);
2470 aSANum = Model->Number(anAreaSA);
2471 aGISUNum = Model->Number(anAreaGISU);
2472 Handle(StepDimTol_DatumTarget) aDT = new StepDimTol_DatumTarget();
2473 aDT->Init(new TCollection_HAsciiString(), new TCollection_HAsciiString("area"), anAreaSA->OfShape(),
2474 anAreaSA->ProductDefinitional(), aTargetId);
2475 Model->ReplaceEntity(aSANum, aDT);
2476 StepAP242_ItemIdentifiedRepresentationUsageDefinition aDefinition;
2477 aDefinition.SetValue(aDT);
2478 anAreaGISU->SetDefinition(aDefinition);
2479 Model->ReplaceEntity(aGISUNum, anAreaGISU);
2480 }
2481 else {
2482 Handle(StepDimTol_PlacedDatumTargetFeature) aPDTF = new StepDimTol_PlacedDatumTargetFeature();
727b5ad9 2483 aPDTF->Init(new TCollection_HAsciiString(), STEPCAFControl_GDTProperty::GetDatumTargetName(aDatumType),
6595eee7 2484 aPDS, StepData_LTrue, aTargetId);
2485 Model->AddWithRefs(aPDTF);
2486 aDatumTarget = aPDTF;
2487 // Datum targets
2488 Handle(StepRepr_PropertyDefinition) aPD = new StepRepr_PropertyDefinition();
2489 StepRepr_CharacterizedDefinition aCDefinition;
2490 aCDefinition.SetValue(aPDTF);
2491 aPD->Init(new TCollection_HAsciiString(), Standard_False, NULL, aCDefinition);
2492 Handle(StepShape_ShapeRepresentationWithParameters) aSRWP = new StepShape_ShapeRepresentationWithParameters();
2493 // Common for all datum targets
2494 StepBasic_Unit aUnit = GetUnit(aRC);
2495 gp_Ax2 aDTAxis = anObject->GetDatumTargetAxis();
2496 Handle(StepGeom_Axis2Placement3d) anA2P3D =
727b5ad9 2497 STEPCAFControl_GDTProperty::GetAxis2Placement3D(aDTAxis);
b0cef606 2498 anA2P3D->SetName(new TCollection_HAsciiString("orientation"));
6595eee7 2499 Handle(StepRepr_HArray1OfRepresentationItem) anItems;
2500 // Process each datum target type
2501 if (aDatumType == XCAFDimTolObjects_DatumTargetType_Point) {
2502 anItems = new StepRepr_HArray1OfRepresentationItem(1, 1);
2503 }
2504 else {
2505 Handle(TCollection_HAsciiString) aTargetValueName;
2506 if (aDatumType == XCAFDimTolObjects_DatumTargetType_Line) {
2507 anItems = new StepRepr_HArray1OfRepresentationItem(1, 2);
2508 aTargetValueName = new TCollection_HAsciiString("target length");
2509 }
2510 else if (aDatumType == XCAFDimTolObjects_DatumTargetType_Rectangle) {
2511 anItems = new StepRepr_HArray1OfRepresentationItem(1, 3);
2512 aTargetValueName = new TCollection_HAsciiString("target length");
2513 // Additional value
2514 Handle(StepRepr_ReprItemAndMeasureWithUnit) aTargetValue = CreateDimValue(anObject->GetDatumTargetWidth(),
2515 aUnit, new TCollection_HAsciiString("target width"), "POSITIVE_LENGTH_MEASURE", Standard_False);
2516 anItems->SetValue(2, aTargetValue);
2517 Model->AddWithRefs(aTargetValue);
2518 }
2519 else if (aDatumType == XCAFDimTolObjects_DatumTargetType_Circle) {
2520 anItems = new StepRepr_HArray1OfRepresentationItem(1, 2);
2521 aTargetValueName = new TCollection_HAsciiString("target diameter");
2522 }
2523 // Value
2524 Handle(StepRepr_ReprItemAndMeasureWithUnit) aTargetValue= CreateDimValue(anObject->GetDatumTargetLength(),
2525 aUnit, aTargetValueName, "POSITIVE_LENGTH_MEASURE", Standard_False);
2526 anItems->SetValue(1, aTargetValue);
2527 Model->AddWithRefs(aTargetValue);
2528 }
2529 anItems->SetValue(anItems->Length(), anA2P3D);
2530 aSRWP->Init(new TCollection_HAsciiString(), anItems, aRC);
2531
2532 // Create and write auxiliary entities
2533 Handle (StepShape_ShapeDefinitionRepresentation) aSDR = new StepShape_ShapeDefinitionRepresentation();
2534 StepRepr_RepresentedDefinition aRDefinition;
2535 aRDefinition.SetValue(aPD);
2536 aSDR->Init(aRDefinition, aSRWP);
2537 Model->AddWithRefs(aPD);
2538 Model->AddWithRefs(aSRWP);
2539 Model->AddWithRefs(aSDR);
2540 }
2541 // Link datum target to datum feature
2542 Handle(StepRepr_FeatureForDatumTargetRelationship) aFFDTR = new StepRepr_FeatureForDatumTargetRelationship();
2543 aFFDTR->Init(new TCollection_HAsciiString(), Standard_False, NULL, aDatumTarget, aSA);
2544 Model->AddWithRefs(aFFDTR);
2545 aSA = aDatumTarget;
2546 }
2547
2548 // Datum
2549 Handle(StepDimTol_Datum) aDatum = theWrittenDatum;
2550 if (isFirstDTarget) {
2551 aDatum = new StepDimTol_Datum();
2552 aDatum->Init(new TCollection_HAsciiString(), new TCollection_HAsciiString(), aPDS, StepData_LTrue, anIdentifier);
2553 Model->AddWithRefs(aDatum);
2554 }
2555
2556 // Shape_Aspect_Relationship
2557 Handle(StepRepr_ShapeAspectRelationship) aSAR = new StepRepr_ShapeAspectRelationship();
2558 aSAR->Init(new TCollection_HAsciiString(), Standard_False, NULL, aSA, aDatum);
2559 Model->AddWithRefs(aSAR);
2560
2561 // Auxiliary entities
2562 // Property_Definition
2563 Handle(StepRepr_PropertyDefinition) aPD = new StepRepr_PropertyDefinition();
2564 Handle(TCollection_HAsciiString) aPDName = new TCollection_HAsciiString("Datum Feature Symbol ");
2565 aPDName = aPDName->Cat(anIdentifier)->Cat(aTargetId);
2566 StepRepr_CharacterizedDefinition aCD;
2567 aCD.SetValue(aSA);
2568 aPD->Init(aPDName, Standard_False, NULL, aCD);
2569 Model->AddWithRefs(aPD);
2570 // Shape_Representation
2571 Handle(StepShape_ShapeRepresentation) aShapeRepr = new StepShape_ShapeRepresentation();
2572 aShapeRepr->Init(aPDName, aGISU->IdentifiedItem(), aRC);
2573 Model->AddWithRefs(aShapeRepr);
2574 // Shape_Definition_Representation
2575 Handle (StepShape_ShapeDefinitionRepresentation) aSDR = new StepShape_ShapeDefinitionRepresentation();
2576 StepRepr_RepresentedDefinition aRDefinition;
2577 aRDefinition.SetValue(aPD);
2578 aSDR->Init(aRDefinition, aShapeRepr);
2579 Model->AddWithRefs(aSDR);
2580
b0cef606 2581 //Annotation plane and Presentation
f0bf70e8 2582 WritePresentation(WS, anObject->GetPresentation(), anObject->GetPlane(), anObject->GetPointTextAttach(), aSA);
b0cef606 2583
6595eee7 2584 return aDatum;
2585}
2586
2587//=======================================================================
2588//function : WriteDimValues
2589//purpose : auxiliary (write all data for given dimension: values,
2590// qualifiers, modifiers, orientation and tolerance class)
2591//======================================================================
2592static void WriteDimValues(const Handle(XSControl_WorkSession) &WS,
2593 const Handle(XCAFDimTolObjects_DimensionObject) theObject,
2594 const Handle(StepRepr_RepresentationContext) theRC,
2595 const StepShape_DimensionalCharacteristic theDimension)
2596{
2597 // Get working data
7f56eba8 2598 const Handle(Interface_InterfaceModel) &aModel = WS->Model();
6595eee7 2599 XCAFDimTolObjects_DimensionModifiersSequence aModifiers = theObject->GetModifiers();
2600 Handle(Standard_Transient) aDim = theDimension.Value();
2601 Standard_Boolean isAngle = aDim->IsKind(STANDARD_TYPE(StepShape_AngularLocation)) ||
2602 aDim->IsKind(STANDARD_TYPE(StepShape_AngularSize));
2603
2604 // Unit
2605 StepBasic_Unit aUnit = GetUnit(theRC, isAngle);
2606 Standard_CString aMeasureName;
2607 if (isAngle)
2608 aMeasureName = "POSITIVE_PLANE_ANGLE_MEASURE";
2609 else
2610 aMeasureName = "POSITIVE_LENGTH_MEASURE";
2611
2612 // Values
2613 Handle(StepRepr_HArray1OfRepresentationItem) aValues;
7644c7f4 2614 Standard_Integer aNbItems = 1, aValIt = 1;
6595eee7 2615 if (theObject->IsDimWithRange())
7644c7f4 2616 aNbItems += 2;
6595eee7 2617 if (aModifiers.Length() > 0)
7644c7f4 2618 aNbItems++;
6595eee7 2619 if (theObject->GetType() == XCAFDimTolObjects_DimensionType_Location_Oriented)
7644c7f4 2620 aNbItems++;
2621 aNbItems += theObject->NbDescriptions();
2622 aValues = new StepRepr_HArray1OfRepresentationItem(1, aNbItems);
6595eee7 2623
2624 // Nominal value
2625 Standard_Real aNominal = theObject->GetValue();
2626 Standard_Integer aLeftNbDigits, aRightNbDigits;
2627 theObject->GetNbOfDecimalPlaces(aLeftNbDigits, aRightNbDigits);
2628 Standard_Integer aNbQualifiers = 0;
2629 if (theObject->HasQualifier() && !isAngle)
2630 aNbQualifiers++;
2631 if (aLeftNbDigits > 0 || aRightNbDigits > 0)
2632 aNbQualifiers++;
2633 // With qualifiers
2634 if (aNbQualifiers > 0) {
2635 Handle(StepShape_QualifiedRepresentationItem) aQRI = new StepShape_QualifiedRepresentationItem();
2636 Handle(StepShape_HArray1OfValueQualifier) aQualifiers = new StepShape_HArray1OfValueQualifier(1, aNbQualifiers);
2637 // Type qualifier
2638 if (theObject->HasQualifier() && !isAngle) {
2639 StepShape_ValueQualifier anItem;
2640 Handle(StepShape_TypeQualifier) aType = new StepShape_TypeQualifier();
2641 XCAFDimTolObjects_DimensionQualifier aQualifier = theObject->GetQualifier();
727b5ad9 2642 aType->Init(STEPCAFControl_GDTProperty::GetDimQualifierName(aQualifier));
6595eee7 2643 aModel->AddWithRefs(aType);
2644 anItem.SetValue(aType);
2645 aQualifiers->SetValue(1, anItem);
2646 }
2647 // Number of decimal places
2648 if (aLeftNbDigits > 0 || aRightNbDigits > 0) {
2649 StepShape_ValueQualifier anItem;
2650 Handle(StepShape_ValueFormatTypeQualifier) aType = new StepShape_ValueFormatTypeQualifier();
2651 Handle(TCollection_HAsciiString) aFormatType = new TCollection_HAsciiString("NR2 ");
2652 aFormatType->AssignCat(new TCollection_HAsciiString(aLeftNbDigits));
2653 aFormatType->AssignCat(new TCollection_HAsciiString("."));
2654 aFormatType->AssignCat(new TCollection_HAsciiString(aRightNbDigits));
2655 aType->Init(aFormatType);
2656 aModel->AddWithRefs(aType);
2657 anItem.SetValue(aType);
2658 aQualifiers->SetValue(aNbQualifiers, anItem);
2659 }
2660 // Set qualifiers
2661 aQRI->SetQualifiers(aQualifiers);
2662 Handle(StepRepr_ReprItemAndMeasureWithUnit) anItem = CreateDimValue(aNominal, aUnit,
2663 new TCollection_HAsciiString("nominal value"), aMeasureName, isAngle, Standard_True, aQRI);
7644c7f4 2664 aValues->SetValue(aValIt, anItem);
2665 aValIt++;
6595eee7 2666 }
2667 // Without qualifiers
2668 else {
2669 Handle(StepRepr_ReprItemAndMeasureWithUnit) anItem = CreateDimValue(aNominal, aUnit,
2670 new TCollection_HAsciiString("nominal value"), aMeasureName, isAngle);
7644c7f4 2671 aValues->SetValue(aValIt, anItem);
2672 aValIt++;
6595eee7 2673 }
7644c7f4 2674
6595eee7 2675 // Ranges
2676 if (theObject->IsDimWithRange()) {
2677 Handle(StepRepr_ReprItemAndMeasureWithUnit) aLowerItem = CreateDimValue(theObject->GetLowerBound(), aUnit,
2678 new TCollection_HAsciiString("lower limit"), aMeasureName, isAngle);
2679 Handle(StepRepr_ReprItemAndMeasureWithUnit) anUpperItem = CreateDimValue(theObject->GetUpperBound(), aUnit,
2680 new TCollection_HAsciiString("upper limit"), aMeasureName, isAngle);
7644c7f4 2681 aValues->SetValue(aValIt, aLowerItem);
2682 aValIt++;
2683 aValues->SetValue(aValIt, anUpperItem);
2684 aValIt++;
6595eee7 2685 }
2686
2687 // Modifiers
2688 if (aModifiers.Length() > 0) {
2689 Handle(StepRepr_CompoundRepresentationItem) aCompoundRI = new StepRepr_CompoundRepresentationItem();
2690 Handle (StepRepr_HArray1OfRepresentationItem) aModifItems =
2691 new StepRepr_HArray1OfRepresentationItem(1, aModifiers.Length());
2692 for (Standard_Integer i = 1; i <= aModifiers.Length(); i++) {
2693 XCAFDimTolObjects_DimensionModif aModif = aModifiers.Value(i);
2694 Handle(StepRepr_DescriptiveRepresentationItem) aModifItem =
2695 new StepRepr_DescriptiveRepresentationItem();
727b5ad9 2696 aModifItem->Init(new TCollection_HAsciiString(), STEPCAFControl_GDTProperty::GetDimModifierName(aModif));
6595eee7 2697 aModel->AddWithRefs(aModifItem);
2698 aModifItems->SetValue(i, aModifItem);
2699 }
2700 aCompoundRI->Init(new TCollection_HAsciiString(), aModifItems);
7644c7f4 2701 aValues->SetValue(aValIt, aCompoundRI);
2702 aValIt++;
6595eee7 2703 }
2704
2705 // Orientation
2706 if (theObject->GetType() == XCAFDimTolObjects_DimensionType_Location_Oriented) {
2707 Handle(StepGeom_Axis2Placement3d) anOrientation = new StepGeom_Axis2Placement3d();
2708 gp_Dir aDir;
2709 theObject->GetDirection(aDir);
b7b2f85a 2710 GeomToStep_MakeCartesianPoint MkPoint(gp_Pnt(0, 0, 0));
18f7c9a5 2711 Handle(StepGeom_CartesianPoint) aLoc = MkPoint.Value();
6595eee7 2712 Handle(StepGeom_Direction) anAxis = new StepGeom_Direction();
2713 Handle(TColStd_HArray1OfReal) aCoords = new TColStd_HArray1OfReal(1, 3);
2714 aCoords->SetValue(1, aDir.X());
2715 aCoords->SetValue(2, aDir.Y());
2716 aCoords->SetValue(3, aDir.Z());
2717 anAxis->Init(new TCollection_HAsciiString(), aCoords);
2718 anOrientation->Init(new TCollection_HAsciiString("orientation"), aLoc, Standard_True, anAxis, Standard_False, NULL);
7644c7f4 2719 aValues->SetValue(aValIt, anOrientation);
2720 aValIt++;
2721 }
2722
2723 // Descriptions
2724 if (theObject->HasDescriptions()) {
2725 for (Standard_Integer i = 0; i < theObject->NbDescriptions(); i++) {
2726 Handle(StepRepr_DescriptiveRepresentationItem) aDRI = new StepRepr_DescriptiveRepresentationItem();
2727 aDRI->Init(theObject->GetDescriptionName(i), theObject->GetDescription(i));
2728 aValues->SetValue(aValIt, aDRI);
2729 aValIt++;
2730 }
6595eee7 2731 }
2732
2733 for (Standard_Integer i = 1; i <= aValues->Length(); i++)
2734 aModel->AddWithRefs(aValues->Value(i));
2735
2736 // Create resulting Shape_Dimension_Representation
2737 Handle(StepShape_ShapeDimensionRepresentation) aSDR = new StepShape_ShapeDimensionRepresentation();
2738 aSDR->Init(new TCollection_HAsciiString(), aValues, theRC);
2739 aModel->AddWithRefs(aSDR);
2740 Handle(StepShape_DimensionalCharacteristicRepresentation) aDCR = new StepShape_DimensionalCharacteristicRepresentation();
2741 aDCR->Init(theDimension, aSDR);
2742 aModel->AddWithRefs(aDCR);
2743
2744 // Plus_Minus_Tolerance
2745 if (theObject->IsDimWithPlusMinusTolerance()) {
2746 Handle(TCollection_HAsciiString) aDummyName = new TCollection_HAsciiString(aMeasureName);
2747 aDummyName = aDummyName->SubString(9, aDummyName->Length()); //delete "POSITIVE_"
2748 aMeasureName = aDummyName->ToCString();
2749 Standard_Real aLowerTolValue = -theObject->GetLowerTolValue(),
2750 anUpperTolValue = theObject->GetUpperTolValue();
2751 // Upper
2752 Handle(StepBasic_MeasureWithUnit) anUpperMWU = new StepBasic_MeasureWithUnit();
2753 Handle(StepBasic_MeasureValueMember) anUpperValue = new StepBasic_MeasureValueMember();
2754 anUpperValue->SetName(aMeasureName);
2755 anUpperValue->SetReal(anUpperTolValue);
2756 anUpperMWU->Init(anUpperValue, aUnit);
2757 aModel->AddWithRefs(anUpperMWU);
2758 // Lower
2759 Handle(StepBasic_MeasureWithUnit) aLowerMWU = new StepBasic_MeasureWithUnit();
2760 Handle(StepBasic_MeasureValueMember) aLowerValue = new StepBasic_MeasureValueMember();
2761 aLowerValue->SetName(aMeasureName);
2762 aLowerValue->SetReal(aLowerTolValue);
2763 aLowerMWU->Init(aLowerValue, aUnit);
2764 aModel->AddWithRefs(aLowerMWU);
2765 // Tolerance
2766 Handle(StepShape_ToleranceValue) aTolValue = new StepShape_ToleranceValue();
2767 aTolValue->Init(aLowerMWU, anUpperMWU);
2768 aModel->AddWithRefs(aTolValue);
2769 StepShape_ToleranceMethodDefinition aMethod;
2770 aMethod.SetValue(aTolValue);
2771 Handle(StepShape_PlusMinusTolerance) aPlusMinusTol = new StepShape_PlusMinusTolerance();
2772 aPlusMinusTol->Init(aMethod, theDimension);
2773 aModel->AddWithRefs(aPlusMinusTol);
2774 }
2775 // Tolerance class
2776 if (theObject->IsDimWithClassOfTolerance()) {
2777 Standard_Boolean isHole;
2778 XCAFDimTolObjects_DimensionFormVariance aFormVariance;
2779 XCAFDimTolObjects_DimensionGrade aGrade;
2780 if (!theObject->GetClassOfTolerance(isHole, aFormVariance, aGrade))
2781 return;
727b5ad9 2782 Handle(StepShape_LimitsAndFits) aLAF = STEPCAFControl_GDTProperty::GetLimitsAndFits(isHole, aFormVariance, aGrade);
6595eee7 2783 aModel->AddWithRefs(aLAF);
2784 StepShape_ToleranceMethodDefinition aMethod;
2785 aMethod.SetValue(aLAF);
2786 Handle(StepShape_PlusMinusTolerance) aPlusMinusTol = new StepShape_PlusMinusTolerance();
2787 aPlusMinusTol->Init(aMethod, theDimension);
2788 aModel->AddWithRefs(aPlusMinusTol);
2789 }
2790}
2791
2792//=======================================================================
b7b2f85a 2793//function : WriteDerivedGeometry
2794//purpose : auxiliary (write connection point for dimensions)
2795//======================================================================
2796static void WriteDerivedGeometry (const Handle(XSControl_WorkSession) &WS,
2797 const Handle(XCAFDimTolObjects_DimensionObject)& theObject,
2798 const Handle(StepRepr_ConstructiveGeometryRepresentation) theRepr,
2799 Handle(StepRepr_ShapeAspect)& theFirstSA,
2800 Handle(StepRepr_ShapeAspect)& theSecondSA,
2801 NCollection_Vector<Handle(StepGeom_CartesianPoint)>& thePnts)
2802{
2803 const Handle(Interface_InterfaceModel) &aModel = WS->Model();
2804 // First point
2805 if (theObject->HasPoint()) {
2806 GeomToStep_MakeCartesianPoint aPointMaker(theObject->GetPoint());
2807 Handle(StepGeom_CartesianPoint) aPoint = aPointMaker.Value();
2808 thePnts.Append(aPoint);
2809 Handle(StepRepr_DerivedShapeAspect) aDSA = new StepRepr_DerivedShapeAspect();
2810 aDSA->Init(new TCollection_HAsciiString(), new TCollection_HAsciiString(), theFirstSA->OfShape(), StepData_LFalse);
2811 Handle(StepAP242_GeometricItemSpecificUsage) aGISU = new StepAP242_GeometricItemSpecificUsage();
2812 StepAP242_ItemIdentifiedRepresentationUsageDefinition aDefinition;
2813 aDefinition.SetValue(aDSA);
2814 Handle(StepRepr_HArray1OfRepresentationItem) anItem = new StepRepr_HArray1OfRepresentationItem(1, 1);
2815 anItem->SetValue(1, aPoint);
2816 aGISU->Init(new TCollection_HAsciiString(), new TCollection_HAsciiString(), aDefinition, theRepr, anItem);
2817 Handle(StepRepr_ShapeAspectDerivingRelationship) aSADR = new StepRepr_ShapeAspectDerivingRelationship();
2818 aSADR->Init(new TCollection_HAsciiString(), Standard_False, new TCollection_HAsciiString(), aDSA, theFirstSA);
2819 theFirstSA = aDSA;
2820 aModel->AddWithRefs(aGISU);
2821 aModel->AddWithRefs(aSADR);
2822 }
2823
2824 // Second point (for locations)
2825 if (theObject->HasPoint2()) {
2826 GeomToStep_MakeCartesianPoint aPointMaker(theObject->GetPoint2());
2827 Handle(StepGeom_CartesianPoint) aPoint = aPointMaker.Value();
2828 thePnts.Append(aPoint);
2829 Handle(StepRepr_DerivedShapeAspect) aDSA = new StepRepr_DerivedShapeAspect();
2830 aDSA->Init(new TCollection_HAsciiString(), new TCollection_HAsciiString(), theFirstSA->OfShape(), StepData_LFalse);
2831 Handle(StepAP242_GeometricItemSpecificUsage) aGISU = new StepAP242_GeometricItemSpecificUsage();
2832 StepAP242_ItemIdentifiedRepresentationUsageDefinition aDefinition;
2833 aDefinition.SetValue(aDSA);
2834 Handle(StepRepr_HArray1OfRepresentationItem) anItem = new StepRepr_HArray1OfRepresentationItem(1, 1);
2835 anItem->SetValue(1, aPoint);
2836 aGISU->Init(new TCollection_HAsciiString(), new TCollection_HAsciiString(), aDefinition, theRepr, anItem);
2837 Handle(StepRepr_ShapeAspectDerivingRelationship) aSADR = new StepRepr_ShapeAspectDerivingRelationship();
2838 aSADR->Init(new TCollection_HAsciiString(), Standard_False, new TCollection_HAsciiString(), aDSA, theSecondSA);
2839 theSecondSA = aDSA;
2840 aModel->AddWithRefs(aGISU);
2841 aModel->AddWithRefs(aSADR);
2842 }
2843}
2844
2845//=======================================================================
6595eee7 2846//function : WriteDatumSystem
2847//purpose : auxiliary (write Write datum system for given
2848// geometric_tolerance)
2849//======================================================================
2850static Handle(StepDimTol_HArray1OfDatumSystemOrReference) WriteDatumSystem(const Handle(XSControl_WorkSession) &WS,
2851 const TDF_Label theGeomTolL,
2852 const TDF_LabelSequence theDatumSeq,
2853 const STEPConstruct_DataMapOfAsciiStringTransient theDatumMap,
2854 const Handle(StepRepr_RepresentationContext)& theRC)
2855{
2856 // Get working data
7f56eba8 2857 const Handle(Interface_InterfaceModel) &Model = WS->Model();
6595eee7 2858 const Handle(Interface_HGraph) aHGraph = WS->HGraph();
2859 if (aHGraph.IsNull())
2860 return NULL;
2861 Interface_Graph aGraph = aHGraph->Graph();
2862 Handle(XCAFDoc_GeomTolerance) aGTAttr;
2863 if (!theGeomTolL.FindAttribute(XCAFDoc_GeomTolerance::GetID(), aGTAttr))
2864 return NULL;
2865 Handle(XCAFDimTolObjects_GeomToleranceObject) anObject = aGTAttr->GetObject();
2866 if (anObject.IsNull())
2867 return NULL;
2868
2869 // Unit
2870 StepBasic_Unit aUnit = GetUnit(theRC);
2871
2872 XCAFDimTolObjects_DatumObjectSequence aDatums;
2873 Standard_Integer aMaxDatumNum = 0;
2874 for (Standard_Integer i = 1; i <= theDatumSeq.Length(); i++) {
2875 Handle(XCAFDoc_Datum) aDatumAttr;
2876 if (!theDatumSeq.Value(i).FindAttribute(XCAFDoc_Datum::GetID(), aDatumAttr))
2877 continue;
2878 Handle(XCAFDimTolObjects_DatumObject) aDatumObj = aDatumAttr->GetObject();
2879 if (aDatumObj.IsNull())
2880 continue;
2881 aDatums.Append(aDatumObj);
2882 aMaxDatumNum = Max(aMaxDatumNum, aDatumObj->GetPosition());
2883 }
c1070321 2884 if (aMaxDatumNum == 0)
2885 return NULL;
6595eee7 2886
2887 Handle(StepDimTol_HArray1OfDatumReferenceCompartment) aConstituents =
2888 new StepDimTol_HArray1OfDatumReferenceCompartment(1, aMaxDatumNum);
2889 // Auxiliary datum to initialize attributes in Datum_System
2890 Handle(StepDimTol_Datum) aFirstDatum;
2891 Standard_Integer aConstituentsNum = 0;
2892 for (Standard_Integer i = 1; i <= aMaxDatumNum; i++) {
2893 // Collect datums with i-th position
2894 XCAFDimTolObjects_DatumObjectSequence aDatumSeqPos;
2895 for (Standard_Integer j = 1; j <= aDatums.Length(); j++)
2896 if (aDatums.Value(j)->GetPosition() == i)
2897 aDatumSeqPos.Append(aDatums.Value(j));
2898 if (aDatumSeqPos.Length() < 1)
2899 continue;
2900
2901 aConstituentsNum++;
2902 // Initialize Datum_Reference_Compartment
2903 StepDimTol_DatumOrCommonDatum aDatumRef;
2904 Handle(StepDimTol_DatumReferenceCompartment) aCompartment =
2905 new StepDimTol_DatumReferenceCompartment();
2906 Handle(StepDimTol_HArray1OfDatumReferenceModifier) aModifiers;
2907 if (aDatumSeqPos.Length() == 1) {
2908 // Datum entity
aa00364d 2909 Handle(Standard_Transient) aFDValue;
f3ec3b37 2910 if (theDatumMap.Find(aDatumSeqPos.Value(1)->GetName()->String(), aFDValue) && !aFDValue.IsNull())
aa00364d 2911 aFirstDatum = Handle(StepDimTol_Datum)::DownCast (aFDValue);
6595eee7 2912 aDatumRef.SetValue(aFirstDatum);
2913 // Modifiers
2914 XCAFDimTolObjects_DatumModifiersSequence aSimpleModifiers = aDatumSeqPos.Value(1)->GetModifiers();
2915 XCAFDimTolObjects_DatumModifWithValue aModifWithVal;
2916 Standard_Real aValue = 0;
2917 aDatumSeqPos.Value(1)->GetModifierWithValue(aModifWithVal, aValue);
727b5ad9 2918 aModifiers = STEPCAFControl_GDTProperty::GetDatumRefModifiers(aSimpleModifiers, aModifWithVal, aValue, aUnit);
6595eee7 2919 // Add Datum_Reference_Modifier_With_Value
2920 if (!aModifiers.IsNull()) {
2921 Handle(StepDimTol_DatumReferenceModifierWithValue) aDRMWV =
2922 aModifiers->Value(aModifiers->Length()).DatumReferenceModifierWithValue();
2923 if (!aDRMWV.IsNull()) {
2924 Model->AddWithRefs(aDRMWV);
2925 }
2926 }
2927 }
2928 else {
2929 Handle(StepDimTol_HArray1OfDatumReferenceElement) aCommonDatumList = new StepDimTol_HArray1OfDatumReferenceElement(1, aDatumSeqPos.Length());
2930 for (Standard_Integer j = 1; j <= aDatumSeqPos.Length(); j++) {
2931 // Datum entity
2932 Handle(StepDimTol_Datum) aDatum;
aa00364d 2933 Handle(Standard_Transient) aDValue;
2934 if (theDatumMap.Find(aDatumSeqPos.Value(j)->GetName()->String(), aDValue))
2935 aDatum = Handle(StepDimTol_Datum)::DownCast (aDValue);
6595eee7 2936 StepDimTol_DatumOrCommonDatum anElemDatumRef;
2937 anElemDatumRef.SetValue(aDatum);
2938 if (aFirstDatum.IsNull())
2939 aFirstDatum = aDatum;
2940 // Modifiers
2941 XCAFDimTolObjects_DatumModifiersSequence aSimpleModifiers = aDatumSeqPos.Value(j)->GetModifiers();
2942 XCAFDimTolObjects_DatumModifWithValue aModifWithVal;
2943 Standard_Real aValue = 0;
2944 aDatumSeqPos.Value(j)->GetModifierWithValue(aModifWithVal, aValue);
2945 Handle(StepDimTol_HArray1OfDatumReferenceModifier) anElemModifiers =
727b5ad9 2946 STEPCAFControl_GDTProperty::GetDatumRefModifiers(aSimpleModifiers, aModifWithVal, aValue, aUnit);
6595eee7 2947 // Add Datum_Reference_Modifier_With_Value
2948 if (!anElemModifiers.IsNull()) {
2949 Handle(StepDimTol_DatumReferenceModifierWithValue) aDRMWV =
f3ec3b37 2950 anElemModifiers->Value(anElemModifiers->Length()).DatumReferenceModifierWithValue();
6595eee7 2951 if (!aDRMWV.IsNull()) {
2952 Model->AddWithRefs(aDRMWV);
2953 }
2954 }
2955 // Datum_Reference_Element
2956 Handle(StepDimTol_DatumReferenceElement) anElement = new StepDimTol_DatumReferenceElement();
2957 anElement->Init(new TCollection_HAsciiString(), new TCollection_HAsciiString(), aDatum->OfShape(),
2958 aDatum->ProductDefinitional(), anElemDatumRef, !anElemModifiers.IsNull(), anElemModifiers);
2959 Model->AddWithRefs(anElement);
2960 aCommonDatumList->SetValue(j, anElement);
2961 }
2962 aDatumRef.SetValue(aCommonDatumList);
2963 }
2964 aCompartment->Init(new TCollection_HAsciiString(), new TCollection_HAsciiString(), aFirstDatum->OfShape(),
2965 aFirstDatum->ProductDefinitional(), aDatumRef, !aModifiers.IsNull(), aModifiers);
2966 Model->AddWithRefs(aCompartment);
2967 aConstituents->SetValue(aConstituentsNum, aCompartment);
2968 }
2969 // Remove null elements from aConstituents
2970 Standard_Integer aNbConstituents = 0;
2971 for (Standard_Integer i = 1; i <= aConstituents->Length(); i++)
2972 if (!aConstituents->Value(i).IsNull())
2973 aNbConstituents++;
2974 Handle(StepDimTol_HArray1OfDatumReferenceCompartment) aResConstituents =
2975 new StepDimTol_HArray1OfDatumReferenceCompartment(1, aNbConstituents);
2976 Standard_Integer aConstituentsIt = 0;
2977 for (Standard_Integer i = 1; i <= aConstituents->Length(); i++)
2978 if (!aConstituents->Value(i).IsNull()) {
2979 aConstituentsIt++;
2980 aResConstituents->SetValue(aConstituentsIt, aConstituents->Value(i));
2981 }
2982
2983 Handle(StepDimTol_HArray1OfDatumSystemOrReference) aDatumSystem;
2984 Handle(StepDimTol_DatumSystem) aDS = new StepDimTol_DatumSystem();
2985 aDS->Init(new TCollection_HAsciiString(), new TCollection_HAsciiString(), aFirstDatum->OfShape(),
2986 aFirstDatum->ProductDefinitional(), aResConstituents);
2987 Model->AddWithRefs(aDS);
2988 StepDimTol_DatumSystemOrReference anArrayValue;
2989 anArrayValue.SetValue(aDS);
2990 aDatumSystem = new StepDimTol_HArray1OfDatumSystemOrReference(1, 1);
2991 aDatumSystem->SetValue(1, anArrayValue);
2992
2993 // Axis
2994 if (anObject->HasAxis()) {
2995 Handle(StepGeom_Axis2Placement3d) anAxis =
727b5ad9 2996 STEPCAFControl_GDTProperty::GetAxis2Placement3D(anObject->GetAxis());
b0cef606 2997 anAxis->SetName(new TCollection_HAsciiString("orientation"));
6595eee7 2998 Handle(StepAP242_GeometricItemSpecificUsage) aGISU = new StepAP242_GeometricItemSpecificUsage();
2999 StepAP242_ItemIdentifiedRepresentationUsageDefinition aDefinition;
3000 aDefinition.SetValue(aDS);
3001 Handle(StepRepr_HArray1OfRepresentationItem) anReprItems = new StepRepr_HArray1OfRepresentationItem(1, 1);
a9dde4a3 3002 Handle(StepRepr_RepresentationItem) anIdentifiedItem = anAxis;
6595eee7 3003 anReprItems->SetValue(1, anIdentifiedItem);
3004 Interface_EntityIterator subs = aGraph.Sharings(aFirstDatum->OfShape());
3005 Handle(StepShape_ShapeDefinitionRepresentation) aSDR;
3006 for (subs.Start(); subs.More() && aSDR.IsNull(); subs.Next()) {
3007 Handle(Standard_Transient) anEntity = subs.Value();
3008 aSDR = Handle(StepShape_ShapeDefinitionRepresentation)::DownCast(anEntity);
3009 }
3010 if (aSDR.IsNull())
3011 return aDatumSystem;
3012
3013 aGISU->Init(new TCollection_HAsciiString(), new TCollection_HAsciiString(),
3014 aDefinition, aSDR->UsedRepresentation(), anReprItems);
3015 Model->AddWithRefs(anAxis);
3016 Model->AddWithRefs(aGISU);
3017 }
3018
3019 return aDatumSystem;
3020}
3021
3022//=======================================================================
3023//function : WriteToleranceZone
3024//purpose : auxiliary (write tolerace zones)
3025//=======================================================================
3026static void WriteToleranceZone (const Handle(XSControl_WorkSession) &WS,
3027 const Handle(XCAFDimTolObjects_GeomToleranceObject)& theObject,
3028 const Handle(StepDimTol_GeometricTolerance)& theEntity,
3029 const Handle(StepRepr_RepresentationContext)& theRC)
3030{
3031 // Get working data
7f56eba8 3032 const Handle(Interface_InterfaceModel) &Model = WS->Model();
6595eee7 3033 if (theEntity.IsNull() || theObject.IsNull())
3034 return;
3035
3036 // Return if there is no tolerance zones
3037 if (theObject->GetTypeOfValue() == XCAFDimTolObjects_GeomToleranceTypeValue_None &&
3038 theObject->GetZoneModifier() != XCAFDimTolObjects_GeomToleranceZoneModif_Runout)
3039 return;
3040
3041 // Create Tolerance_Zone
3042 Handle(StepDimTol_ToleranceZoneForm) aForm = new StepDimTol_ToleranceZoneForm();
3043 Model->AddWithRefs(aForm);
727b5ad9 3044 aForm->Init(STEPCAFControl_GDTProperty::GetTolValueType(theObject->GetTypeOfValue()));
6595eee7 3045 Handle(StepDimTol_HArray1OfToleranceZoneTarget) aZoneTargetArray = new StepDimTol_HArray1OfToleranceZoneTarget(1, 1);
3046 StepDimTol_ToleranceZoneTarget aTarget;
3047 aTarget.SetValue(theEntity);
3048 aZoneTargetArray->SetValue(1, aTarget);
3049 Handle(StepDimTol_ToleranceZone) aZone = new StepDimTol_ToleranceZone();
3050 aZone->Init(new TCollection_HAsciiString(), new TCollection_HAsciiString(),
3051 theEntity->TolerancedShapeAspect().ShapeAspect()->OfShape(), StepData_LFalse,
3052 aZoneTargetArray, aForm);
3053 Model->AddWithRefs(aZone);
3054
3055 // Runout_Tolerance_Zone
3056 Handle(StepBasic_PlaneAngleMeasureWithUnit) aPAMWU = new StepBasic_PlaneAngleMeasureWithUnit();
3057 Handle(StepBasic_MeasureValueMember) aValueMember = new StepBasic_MeasureValueMember();
3058 aValueMember->SetName("PLANE_ANGLE_MEASURE");
3059 aValueMember->SetReal(theObject->GetValueOfZoneModifier());
3060 aPAMWU->Init(aValueMember, GetUnit(theRC, Standard_True));
3061 Handle(StepDimTol_RunoutZoneOrientation) anOrientation = new StepDimTol_RunoutZoneOrientation();
3062 anOrientation->Init(aPAMWU);
3063 Handle(StepDimTol_RunoutZoneDefinition) aDefinition = new StepDimTol_RunoutZoneDefinition();
3064 aDefinition->Init(aZone, NULL, anOrientation);
3065 Model->AddWithRefs(aDefinition);
3066 Model->AddWithRefs(anOrientation);
3067 Model->AddWithRefs(aPAMWU);
3068}
3069
3070//=======================================================================
3071//function : WriteGeomTolerance
3072//purpose : auxiliary (write Geometric_Tolerance entity for given shapes,
3073// label and datum system)
3074//======================================================================
3075static void WriteGeomTolerance (const Handle(XSControl_WorkSession) &WS,
3076 const TDF_LabelSequence theShapeSeqL,
3077 const TDF_Label theGeomTolL,
3078 const Handle(StepDimTol_HArray1OfDatumSystemOrReference)& theDatumSystem,
3079 const Handle(StepRepr_RepresentationContext)& theRC)
3080{
3081 // Get working data
7f56eba8 3082 const Handle(Interface_InterfaceModel) &Model = WS->Model();
6595eee7 3083 Handle(XCAFDoc_GeomTolerance) aGTAttr;
3084 if (!theGeomTolL.FindAttribute(XCAFDoc_GeomTolerance::GetID(), aGTAttr))
3085 return;
3086 Handle(XCAFDimTolObjects_GeomToleranceObject) anObject = aGTAttr->GetObject();
3087 if (anObject.IsNull())
3088 return;
3089
3090 // Value
3091 Handle(StepBasic_LengthMeasureWithUnit) aLMWU = new StepBasic_LengthMeasureWithUnit();
3092 StepBasic_Unit aUnit = GetUnit(theRC);
3093 Handle(StepBasic_MeasureValueMember) aValueMember = new StepBasic_MeasureValueMember();
3094 aValueMember->SetName("LENGTH_MEASURE");
3095 aValueMember->SetReal(anObject->GetValue());
3096 aLMWU->Init(aValueMember, aUnit);
3097 Model->AddWithRefs(aLMWU);
3098
3099 // Geometric_Tolerance target
3100 Handle(StepRepr_ShapeAspect) aMainSA;
3101 Handle(StepRepr_RepresentationContext) dummyRC;
3102 Handle(StepAP242_GeometricItemSpecificUsage) dummyGISU;
3103 if (theShapeSeqL.Length() == 1) {
3104 TopoDS_Shape aShape = XCAFDoc_ShapeTool::GetShape(theShapeSeqL.Value(1));
3105 aMainSA = WriteShapeAspect(WS, theGeomTolL, aShape, dummyRC, dummyGISU);
3106 Model->AddWithRefs(aMainSA);
3107 }
3108 else {
3109 Handle(StepRepr_CompositeShapeAspect) aCSA;
3110 for (Standard_Integer i = 1; i <= theShapeSeqL.Length(); i++) {
3111 TopoDS_Shape aShape = XCAFDoc_ShapeTool::GetShape(theShapeSeqL.Value(i));
3112 Handle(StepRepr_ShapeAspect) aSA = WriteShapeAspect(WS, theGeomTolL, aShape, dummyRC, dummyGISU);
3113 if (aSA.IsNull())
3114 continue;
3115 if (aCSA.IsNull()) {
3116 aCSA = new StepRepr_CompositeShapeAspect();
3117 aCSA->Init(aSA->Name(), aSA->Description(), aSA->OfShape(), aSA->ProductDefinitional());
3118 Model->AddWithRefs(aCSA);
3119 }
3120 Handle(StepRepr_ShapeAspectRelationship) aSAR = new StepRepr_ShapeAspectRelationship();
3121 aSAR->Init(new TCollection_HAsciiString(), Standard_False, NULL, aCSA, aSA);
3122 Model->AddWithRefs(aSAR);
3123 }
3124 aMainSA = aCSA;
3125 }
3126 StepDimTol_GeometricToleranceTarget aGTTarget;
3127 aGTTarget.SetValue(aMainSA);
3128
3129 Standard_Boolean isWithModif = Standard_False,
3130 isWithDatRef = Standard_False,
3131 isWithMaxTol = Standard_False;
3132 // Modifiers
3133 // Simple modifiers
3134 XCAFDimTolObjects_GeomToleranceModifiersSequence aModifiers = anObject->GetModifiers();
3135 Handle(StepDimTol_HArray1OfGeometricToleranceModifier) aModifArray;
3136 Handle(StepBasic_LengthMeasureWithUnit) aMaxLMWU;
3137 Standard_Integer aModifNb = aModifiers.Length();
3138 if (anObject->GetMaterialRequirementModifier() != XCAFDimTolObjects_GeomToleranceMatReqModif_None)
3139 aModifNb++;
3140 for (Standard_Integer i = 1; i < aModifiers.Length(); i++)
3141 if (aModifiers.Value(i) == XCAFDimTolObjects_GeomToleranceModif_All_Around ||
3142 aModifiers.Value(i) == XCAFDimTolObjects_GeomToleranceModif_All_Over)
3143 aModifNb--;
3144 if (aModifNb > 0) {
3145 isWithModif = Standard_True;
3146 aModifArray = new StepDimTol_HArray1OfGeometricToleranceModifier(1, aModifNb);
3147 Standard_Integer k = 1;
3148 for (Standard_Integer i = 1; i <= aModifiers.Length(); i++) {
3149 if (aModifiers.Value(i) == XCAFDimTolObjects_GeomToleranceModif_All_Around ||
3150 aModifiers.Value(i) == XCAFDimTolObjects_GeomToleranceModif_All_Over)
3151 continue;
3152 StepDimTol_GeometricToleranceModifier aModif =
727b5ad9 3153 STEPCAFControl_GDTProperty::GetGeomToleranceModifier(aModifiers.Value(i));
6595eee7 3154 aModifArray->SetValue(k, aModif);
3155 k++;
3156 }
3157 if (anObject->GetMaterialRequirementModifier() == XCAFDimTolObjects_GeomToleranceMatReqModif_L) {
3158 aModifArray->SetValue(aModifNb, StepDimTol_GTMLeastMaterialRequirement);
3159 }
3160 else if (anObject->GetMaterialRequirementModifier() == XCAFDimTolObjects_GeomToleranceMatReqModif_M) {
3161 aModifArray->SetValue(aModifNb, StepDimTol_GTMMaximumMaterialRequirement);
3162 }
3163 // Modifier with value
3164 if (anObject->GetMaxValueModifier() != 0) {
3165 isWithMaxTol = Standard_True;
3166 aMaxLMWU = new StepBasic_LengthMeasureWithUnit();
c7854818 3167 Handle(StepBasic_MeasureValueMember) aModifierValueMember = new StepBasic_MeasureValueMember();
3168 aModifierValueMember->SetName("LENGTH_MEASURE");
3169 aModifierValueMember->SetReal(anObject->GetMaxValueModifier());
3170 aMaxLMWU->Init(aModifierValueMember, aUnit);
6595eee7 3171 Model->AddWithRefs(aMaxLMWU);
3172 }
3173 }
3174
3175 // Datum Reference
3176 isWithDatRef = !theDatumSystem.IsNull();
3177
3178 // Collect all attributes
3179 Handle(TCollection_HAsciiString) aName = new TCollection_HAsciiString(),
3180 aDescription = new TCollection_HAsciiString();
3181 Handle(StepDimTol_GeometricToleranceWithDatumReference) aGTWDR =
3182 new StepDimTol_GeometricToleranceWithDatumReference();
3183 aGTWDR->SetDatumSystem(theDatumSystem);
3184 Handle(StepDimTol_GeometricToleranceWithModifiers) aGTWM =
3185 new StepDimTol_GeometricToleranceWithModifiers();
3186 aGTWM->SetModifiers(aModifArray);
3187 StepDimTol_GeometricToleranceType aType =
727b5ad9 3188 STEPCAFControl_GDTProperty::GetGeomToleranceType(anObject->GetType());
6595eee7 3189
3190 // Init and write necessary subtype of Geometric_Tolerance entity
3191 Handle(StepDimTol_GeometricTolerance) aGeomTol;
3192 if (isWithModif) {
3193 if (isWithMaxTol) {
3194 if (isWithDatRef) {
3195 // Geometric_Tolerance & Geometric_Tolerance_With_Datum_Reference &
3196 //Geometric_Tolerance_With_Maximum_Tolerance & Geometric_Tolerance_With_Modifiers
3197 Handle(StepDimTol_GeoTolAndGeoTolWthDatRefAndGeoTolWthMaxTol) aResult =
3198 new StepDimTol_GeoTolAndGeoTolWthDatRefAndGeoTolWthMaxTol();
3199 aResult->Init(aName, aDescription, aLMWU, aGTTarget, aGTWDR, aGTWM, aMaxLMWU, aType);
3200 aGeomTol = aResult;
3201 }
3202 else {
3203 // Geometric_Tolerance & Geometric_Tolerance_With_Maximum_Tolerance & Geometric_Tolerance_With_Modifiers
3204 Handle(StepDimTol_GeoTolAndGeoTolWthMaxTol) aResult =
3205 new StepDimTol_GeoTolAndGeoTolWthMaxTol();
3206 aResult->Init(aName, aDescription, aLMWU, aGTTarget, aGTWM, aMaxLMWU, aType);
3207 aGeomTol = aResult;
3208 }
3209 }
3210 else {
3211 if (isWithDatRef) {
3212 // Geometric_Tolerance & Geometric_Tolerance_With_Datum_Reference & Geometric_Tolerance_With_Modifiers
3213 Handle(StepDimTol_GeoTolAndGeoTolWthDatRefAndGeoTolWthMod) aResult =
3214 new StepDimTol_GeoTolAndGeoTolWthDatRefAndGeoTolWthMod();
3215 aResult->Init(aName, aDescription, aLMWU, aGTTarget, aGTWDR, aGTWM, aType);
3216 aGeomTol = aResult;
3217 }
3218 else {
3219 // Geometric_Tolerance & Geometric_Tolerance_With_Modifiers
3220 Handle(StepDimTol_GeoTolAndGeoTolWthMod) aResult =
3221 new StepDimTol_GeoTolAndGeoTolWthMod();
3222 aResult->Init(aName, aDescription, aLMWU, aGTTarget, aGTWM, aType);
3223 aGeomTol = aResult;
3224 }
3225 }
3226 }
3227 else {
3228 if (isWithDatRef) {
3229 // Geometric_Tolerance & Geometric_Tolerance_With_Datum_Reference
3230 Handle(StepDimTol_GeoTolAndGeoTolWthDatRef) aResult =
3231 new StepDimTol_GeoTolAndGeoTolWthDatRef();
3232 aResult->Init(aName, aDescription, aLMWU, aGTTarget, aGTWDR, aType);
3233 aGeomTol = aResult;
3234 }
3235 else {
3236 // Geometric_Tolerance
3237 Handle(StepDimTol_GeometricTolerance) aResult =
727b5ad9 3238 STEPCAFControl_GDTProperty::GetGeomTolerance(anObject->GetType());
6595eee7 3239 if (!aResult.IsNull()) {
3240 aResult->Init(aName, aDescription, aLMWU, aGTTarget);
3241 aGeomTol = aResult;
3242 }
3243 }
3244 }
3245 Model->AddWithRefs(aGeomTol);
3246 WriteToleranceZone(WS, anObject, aGeomTol, theRC);
b0cef606 3247 //Annotation plane and Presentation
f0bf70e8 3248 WritePresentation(WS, anObject->GetPresentation(), anObject->GetPlane(), anObject->GetPointTextAttach(), aGeomTol);
6595eee7 3249}
7fd59977 3250
3251//=======================================================================
3252//function : WriteDGTs
3253//purpose :
3254//=======================================================================
7fd59977 3255Standard_Boolean STEPCAFControl_Writer::WriteDGTs (const Handle(XSControl_WorkSession) &WS,
3256 const TDF_LabelSequence &labels ) const
3257{
3258
3259 if ( labels.Length() <=0 ) return Standard_False;
6595eee7 3260
7fd59977 3261 // get working data
7f56eba8 3262 const Handle(Interface_InterfaceModel) &Model = WS->Model();
3263 const Handle(XSControl_TransferWriter) &TW = WS->TransferWriter();
3264 const Handle(Transfer_FinderProcess) &FP = TW->FinderProcess();
9e20ed57 3265
3266 const Handle(Interface_HGraph) aHGraph = WS->HGraph();
3267 if(aHGraph.IsNull())
3268 return Standard_False;
3269
3270 Interface_Graph aGraph = aHGraph->Graph();
7fd59977 3271 Handle(XCAFDoc_DimTolTool) DGTTool = XCAFDoc_DocumentTool::DimTolTool( labels(1) );
3272 if(DGTTool.IsNull() ) return Standard_False;
3273
3274 TDF_LabelSequence DGTLabels;
6595eee7 3275
7fd59977 3276 STEPConstruct_DataMapOfAsciiStringTransient DatumMap;
3277
3278 // write Datums
6595eee7 3279 DGTLabels.Clear();
7fd59977 3280 DGTTool->GetDatumLabels(DGTLabels);
3281 if(DGTLabels.Length()<=0) return Standard_False;
3282 Standard_Integer i;
3283 for(i=1; i<=DGTLabels.Length(); i++) {
3284 TDF_Label DatumL = DGTLabels.Value(i);
9ebaae37 3285 TDF_LabelSequence ShapeL;
1c9d3225 3286 TDF_LabelSequence aNullSeq;
3287 if(!DGTTool->GetRefShapeLabel(DatumL,ShapeL,aNullSeq)) continue;
7fd59977 3288 // find target shape
9ebaae37 3289 TopoDS_Shape aShape = XCAFDoc_ShapeTool::GetShape(ShapeL.Value(1));
7fd59977 3290 TopLoc_Location Loc;
3291 TColStd_SequenceOfTransient seqRI;
3292 FindEntities( FP, aShape, Loc, seqRI );
3293 if ( seqRI.Length() <= 0 ) {
3294 FP->Messenger() << "Warning: Cannot find RI for "<<aShape.TShape()->DynamicType()->Name()<<endl;
3295 continue;
3296 }
3297 Handle(StepRepr_ProductDefinitionShape) PDS;
3298 Handle(StepRepr_RepresentationContext) RC;
3299 Handle(Standard_Transient) ent = seqRI.Value(1);
3300 Handle(StepShape_AdvancedFace) AF;
3301 Handle(StepShape_EdgeCurve) EC;
3302 FindPDSforDGT(aGraph,ent,PDS,RC,AF,EC);
3303 if(PDS.IsNull()) continue;
3304 //cout<<"Model->Number(PDS)="<<Model->Number(PDS)<<endl;
3305 Handle(XCAFDoc_Datum) DatumAttr;
3306 if(!DatumL.FindAttribute(XCAFDoc_Datum::GetID(),DatumAttr)) continue;
3307 Handle(TCollection_HAsciiString) aName = DatumAttr->GetName();
3308 Handle(TCollection_HAsciiString) aDescription = DatumAttr->GetDescription();
3309 Handle(TCollection_HAsciiString) anIdentification = DatumAttr->GetIdentification();
3310 Handle(StepDimTol_DatumFeature) DF = new StepDimTol_DatumFeature;
3311 Handle(StepDimTol_Datum) aDatum = new StepDimTol_Datum;
3312 DF->Init(aName, new TCollection_HAsciiString, PDS, StepData_LTrue);
3313 Model->AddWithRefs(DF);
3314 aDatum->Init(aName, new TCollection_HAsciiString, PDS, StepData_LFalse, anIdentification);
3315 Model->AddWithRefs(aDatum);
3316 Handle(StepRepr_ShapeAspectRelationship) SAR = new StepRepr_ShapeAspectRelationship;
3317 SAR->SetName(aName);
3318 SAR->SetRelatingShapeAspect(DF);
3319 SAR->SetRelatedShapeAspect(aDatum);
3320 Model->AddWithRefs(SAR);
3321 // write chain for DatumFeature
3322 StepRepr_CharacterizedDefinition CD;
3323 CD.SetValue(DF);
3324 Handle(StepRepr_PropertyDefinition) PropD = new StepRepr_PropertyDefinition;
3325 PropD->Init(aName,Standard_True,aDescription,CD);
3326 Model->AddWithRefs(PropD);
3327 StepRepr_RepresentedDefinition RD;
3328 RD.SetValue(PropD);
3329 Handle(StepShape_ShapeRepresentation) SR = new StepShape_ShapeRepresentation;
3330 Handle(StepRepr_HArray1OfRepresentationItem) HARI =
3331 new StepRepr_HArray1OfRepresentationItem(1,1);
3332 HARI->SetValue(1,AF);
3333 SR->Init(aName,HARI,RC);
3334 Handle(StepShape_ShapeDefinitionRepresentation) SDR = new StepShape_ShapeDefinitionRepresentation;
3335 SDR->Init(RD,SR);
3336 Model->AddWithRefs(SDR);
3337 // write chain for Datum
3338 StepRepr_CharacterizedDefinition CD1;
3339 CD1.SetValue(aDatum);
3340 Handle(StepRepr_PropertyDefinition) PropD1 = new StepRepr_PropertyDefinition;
3341 PropD1->Init(aName,Standard_True,aDescription,CD1);
3342 Model->AddWithRefs(PropD1);
3343 StepRepr_RepresentedDefinition RD1;
3344 RD1.SetValue(PropD1);
3345 Handle(StepShape_ShapeRepresentation) SR1 = new StepShape_ShapeRepresentation;
3346 Handle(StepRepr_HArray1OfRepresentationItem) HARI1 =
3347 new StepRepr_HArray1OfRepresentationItem(1,1);
3348 HARI1->SetValue(1,AF->FaceGeometry());
3349 SR1->Init(aName,HARI1,RC);
3350 Model->AddWithRefs(SR1);
3351 Handle(StepShape_ShapeDefinitionRepresentation) SDR1 = new StepShape_ShapeDefinitionRepresentation;
3352 SDR1->Init(RD1,SR1);
3353 Model->AddWithRefs(SDR1);
3354 // add created Datum into Map
3355 TCollection_AsciiString stmp(aName->ToCString());
3356 stmp.AssignCat(aDescription->ToCString());
3357 stmp.AssignCat(anIdentification->ToCString());
3358 DatumMap.Bind(stmp,aDatum);
3359 }
3360
3361 // write Tolerances and Dimensions
3362 DGTLabels.Clear();
3363 DGTTool->GetDimTolLabels(DGTLabels);
3364 if(DGTLabels.Length()<=0) return Standard_False;
3365 for(i=1; i<=DGTLabels.Length(); i++) {
3366 TDF_Label DimTolL = DGTLabels.Value(i);
9ebaae37 3367 TDF_LabelSequence ShapeL;
1c9d3225 3368 TDF_LabelSequence aNullSeq;
3369 if(!DGTTool->GetRefShapeLabel(DimTolL,ShapeL,aNullSeq)) continue;
7fd59977 3370 // find target shape
9ebaae37 3371 TopoDS_Shape aShape = XCAFDoc_ShapeTool::GetShape(ShapeL.Value(1));
7fd59977 3372 TopLoc_Lo