internal void Pack(Model model, SU.EntitiesRef entitiesRef) { CompDef componentDefinition = model.components[ComponentName]; // We might be making a forward reference, so guarantee // that the ComponentDefinition has a SketchUp pointer. componentDefinition.GuaranteeReference(); SU.ComponentDefinitionRef componentDefinitionRef = componentDefinition.componentDefinitionRef; SU.ComponentInstanceRef componentInstanceRef = new SU.ComponentInstanceRef(); SU.ComponentDefinitionCreateInstance( componentDefinitionRef, componentInstanceRef); SU.EntitiesAddInstance( entitiesRef, componentInstanceRef, null); SU.ComponentInstanceSetName( componentInstanceRef, InstanceName); SU.ComponentInstanceSetTransform( componentInstanceRef, Transform.SUTransformation); if (MaterialName != null) { Material material = null; try { material = model.materials[MaterialName]; } catch (Exception e) { string msg = "\nCould not find a material named " + MaterialName; throw new Exception(e.Message + msg); } SU.DrawingElementRef drawingElementRef = SU.ComponentInstanceToDrawingElement(componentInstanceRef); SU.DrawingElementSetMaterial( drawingElementRef, material.suMaterialRef); } }
internal CompInst(SU.ComponentInstanceRef instanceRef) { // Get the transform. SU.Transformation transformation = new SU.Transformation(); SU.ComponentInstanceGetTransform(instanceRef, out transformation); Transform = new Transform(transformation); // Get the instance name. SU.StringRef stringRef = new SU.StringRef(); SU.StringCreate(stringRef); SU.ComponentInstanceGetName(instanceRef, stringRef); InstanceName = Convert.ToStringAndRelease(stringRef); // Get the definition name. SU.ComponentDefinitionRef componentDefinitionRef = new SU.ComponentDefinitionRef(); SU.ComponentInstanceGetDefinition(instanceRef, componentDefinitionRef); stringRef = new SU.StringRef(); SU.StringCreate(stringRef); SU.ComponentDefinitionGetName(componentDefinitionRef, stringRef); ComponentName = Convert.ToStringAndRelease(stringRef); // Note that a ComponentInstance can upcast into a DrawingElement. // As such, it can have an instance-wide Material set for it that // SketchUp will use on any Faces that use the defalt Material. // But you cannot set the ComponentInstance's material; you must // upcast first. // Upcast to a DrawingElement and get the material name. SU.DrawingElementRef drawingElementRef = SU.ComponentInstanceToDrawingElement(instanceRef); SU.MaterialRef materialRef = new SU.MaterialRef(); try { SU.DrawingElementGetMaterial(drawingElementRef, materialRef); stringRef = new SU.StringRef(); SU.StringCreate(stringRef); SU.MaterialGetNameLegacyBehavior(materialRef, stringRef); MaterialName = Convert.ToStringAndRelease(stringRef); } catch (SketchUpException e) { if (e.ErrorCode == SU.ErrorNoData) { // Not an error. It just has no material. } else { throw; } } }