internal CompDef( SU.ComponentDefinitionRef componentDefinitionRef) { // Get the name. SU.StringRef stringRef = new SU.StringRef(); SU.StringCreate(stringRef); SU.ComponentDefinitionGetName(componentDefinitionRef, stringRef); Name = Convert.ToStringAndRelease(stringRef); // Get the description. stringRef = new SU.StringRef(); SU.StringCreate(stringRef); SU.ComponentDefinitionGetDescription(componentDefinitionRef, stringRef); Description = Convert.ToStringAndRelease(stringRef); // Get the entities. SU.EntitiesRef entitiesRef = new SU.EntitiesRef(); SU.ComponentDefinitionGetEntities(componentDefinitionRef, entitiesRef); //entities = new Entities(entitiesRef); UnpackEntities(entitiesRef); }
public override void Run(string path) { SU.EntitiesRef entities = SUHelper.Initialize(); SU.GeometryInputRef geometry = new SU.GeometryInputRef(); SU.GeometryInputCreate(geometry); foreach (SU.Point3D p in points) { SU.Point3D pc = p; pc.x *= SU.MetersToInches; pc.y *= SU.MetersToInches; pc.z *= SU.MetersToInches; SU.GeometryInputAddVertex(geometry, pc); } SU.LoopInputRef loop = new SU.LoopInputRef(); SU.LoopInputCreate(loop); SU.LoopInputAddVertexIndex(loop, 0); SU.LoopInputAddVertexIndex(loop, 1); SU.LoopInputAddVertexIndex(loop, 2); SU.LoopInputAddVertexIndex(loop, 3); long faceIndex; SU.GeometryInputAddFace(geometry, loop, out faceIndex); SU.EntitiesFill(entities, geometry, true); SUHelper.Finalize(path + @"\PlainQuad.skp"); }
public static SU.EntitiesRef Initialize() { SU.Initialize(); model = new SU.ModelRef(); SU.ModelCreate(model); SU.EntitiesRef entities = new SU.EntitiesRef(); SU.ModelGetEntities(model, entities); return(entities); }
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); } }
protected void UnpackEntities(SU.EntitiesRef entitiesRef) { // Get the faces. long count; SU.EntitiesGetNumFaces(entitiesRef, out count); SU.FaceRef[] faceRefs = new SU.FaceRef[count]; long len = count; SU.EntitiesGetFaces(entitiesRef, len, faceRefs, out count); foreach (SU.FaceRef faceRef in faceRefs) { Faces.Add(new Face(faceRef)); } // Get the groups. SU.EntitiesGetNumGroups(entitiesRef, out count); SU.GroupRef[] groupRefs = new SU.GroupRef[count]; len = count; SU.EntitiesGetGroups(entitiesRef, len, groupRefs, out count); foreach (SU.GroupRef groupRef in groupRefs) { Groups.Add(new Group(groupRef)); } // Get the instances. SU.EntitiesGetNumInstances(entitiesRef, out count); SU.ComponentInstanceRef[] instanceRefs = new SU.ComponentInstanceRef[count]; len = count; SU.EntitiesGetInstances(entitiesRef, len, instanceRefs, out count); foreach (SU.ComponentInstanceRef instanceRef in instanceRefs) { Instances.Add(new CompInst(instanceRef)); } }
void AddBlade( int n, SU.ComponentDefinitionRef parent, SU.ComponentDefinitionRef child, double twist, double spin) { // Instance the child. SU.ComponentInstanceRef ci = new SU.ComponentInstanceRef(); SU.ComponentDefinitionCreateInstance(child, ci); // Set its transform. SU.Transformation twistTrans = new SU.Transformation(); SU.TransformationRotation( ref twistTrans, new SU.Point3D { x = 0, y = 0, z = 0 }, new SU.Vector3D { x = 0, y = 0, z = 1 }, twist); SU.Transformation spinTrans = new SU.Transformation(); SU.TransformationRotation( ref spinTrans, new SU.Point3D { x = 0, y = 0, z = 0 }, new SU.Vector3D { x = 0, y = 1, z = 0 }, spin); SU.Transformation trans = new SU.Transformation(); SU.TransformationMultiply(ref spinTrans, ref twistTrans, ref trans); SU.ComponentInstanceSetTransform(ci, trans); SU.ComponentInstanceSetName(ci, $"Blade #{n}"); SU.EntitiesRef parentEnts = new SU.EntitiesRef(); SU.ComponentDefinitionGetEntities(parent, parentEnts); SU.EntitiesAddInstance(parentEnts, ci, null); }
internal void Pack(Model model, SU.ModelRef modelRef) { // The SketchUp API appears to add a "persistent ID" to vertices, // edges, and faces, as they are added to definitions and groups. // But it also appears not to do this unless the definition or // group reference has already been added to the model. If you // add those references to the model after creating their contents, // SketchUp will detect the missing IDs on load and "fix" them, // causing SketchUp to ask if you want to save the changes when // you close (even though you won't think you've made any). // // You can see this in the "Edit / Undo Check Validity" menu item // when it happens. Undo it, and then use the "Window / Model info" // dialog, under "Statistics" by pressing the "Fix Problems" // button. You'll get a report on the missing persistent IDs. // // To prevent all this from happening, we add the reference first. GuaranteeReference(); SU.ComponentDefinitionRef[] componentDefinitionsArray = new SU.ComponentDefinitionRef[1]; componentDefinitionsArray[0] = componentDefinitionRef; SU.ModelAddComponentDefinitions( modelRef, 1, componentDefinitionsArray); SU.ComponentDefinitionSetName( componentDefinitionRef, Name); SU.ComponentDefinitionSetDescription( componentDefinitionRef, Description); SU.EntitiesRef myEntitiesRef = new SU.EntitiesRef(); SU.ComponentDefinitionGetEntities(componentDefinitionRef, myEntitiesRef); //entities.Pack(model, myEntitiesRef); Pack(model, myEntitiesRef); }
public override void Run(string path) { SU.EntitiesRef entities = SUHelper.Initialize(); SU.GeometryInputRef geometry = new SU.GeometryInputRef(); SU.GeometryInputCreate(geometry); foreach (SU.Point3D p in points) { SU.Point3D pc = p; pc.x *= SU.MetersToInches; pc.y *= SU.MetersToInches; pc.z *= SU.MetersToInches; SU.GeometryInputAddVertex(geometry, pc); } SU.LoopInputRef loop = new SU.LoopInputRef(); SU.LoopInputCreate(loop); SU.LoopInputAddVertexIndex(loop, 0); SU.LoopInputAddVertexIndex(loop, 1); SU.LoopInputAddVertexIndex(loop, 2); SU.LoopInputAddVertexIndex(loop, 3); long faceIndex; SU.GeometryInputAddFace(geometry, loop, out faceIndex); SU.MaterialRef material = new SU.MaterialRef(); SU.MaterialCreate(material); SU.MaterialSetName(material, "Pure Red"); SU.MaterialSetColor(material, new SU.Color { red = 0xFF, alpha = 0xFF }); SU.MaterialInput materialInput = new SU.MaterialInput(); materialInput.materialRef = material; SU.GeometryInputFaceSetFrontMaterial(geometry, 0, materialInput); SU.EntitiesFill(entities, geometry, true); SUHelper.Finalize(path + @"\RedQuad.skp"); }
internal static void Pack(Model model, SU.EntitiesRef entitiesRef, IList <Face> faces) { SU.GeometryInputRef geometryInputRef = new SU.GeometryInputRef(); SU.GeometryInputCreate(geometryInputRef); int vertexIndex = 0; foreach (Face face in faces) { face.Pack(model, geometryInputRef, ref vertexIndex); } if (faces.Count > 0) { SU.EntitiesFill(entitiesRef, geometryInputRef, true); } SU.GeometryInputRelease(geometryInputRef); }
internal void Pack(Model model, SU.EntitiesRef entitiesRef) { // Faces Face.Pack(model, entitiesRef, Faces); // Groups foreach (Group group in Groups) { group.Pack(model, entitiesRef); } // Component instances foreach (CompInst componentInstance in Instances) { componentInstance.Pack(model, entitiesRef); } }
internal new void Pack(Model model, SU.EntitiesRef entitiesRef) { // The SketchUp API appears to add a "persistent ID" to vertices, // edges, and faces, as they are added to definitions and groups. // But it also appears not to do this unless the definition or // group reference has already been added to the model. If you // add those references to the model after creating their contents, // SketchUp will detect the missing IDs on load and "fix" them, // causing SketchUp to ask if you want to save the changes when // you close (even though you won't think you've made any). // // You can see this in the "Edit / Undo Check Validity" menu item // when it happens. Undo it, and then use the "Window / Model info" // dialog, under "Statistics" by pressing the "Fix Problems" // button. You'll get a report on the missing persistent IDs. // // To prevent all this from happening, we add the reference first. SU.GroupRef groupRef = new SU.GroupRef(); SU.GroupCreate(groupRef); SU.EntitiesAddGroup(entitiesRef, groupRef); SU.EntitiesRef myEntitiesRef = new SU.EntitiesRef(); SU.GroupGetEntities(groupRef, myEntitiesRef); //entities.Pack(model, myEntitiesRef); base.Pack(model, myEntitiesRef); // Set the group's name and transformation.. SU.GroupSetName( groupRef, Name); SU.GroupSetTransform( groupRef, Transform.SUTransformation); }
/// <summary> /// Construct a Model from a SketchUp file. /// </summary> /// <param name="path">SketchUp file path.</param> public Model(string path) { SU.Initialize(); SU.ModelRef modelRef = new SU.ModelRef(); SU.ModelCreateFromFile(modelRef, path); UnpackMaterials(modelRef); UnpackComponents(modelRef); SU.EntitiesRef entitiesRef = new SU.EntitiesRef(); SU.ModelGetEntities(modelRef, entitiesRef); UnpackEntities(entitiesRef); SU.ModelRelease(modelRef); SU.Terminate(); }
public override void Run(string path) { SU.EntitiesRef entities = SUHelper.Initialize(); SU.GeometryInputRef geometry = new SU.GeometryInputRef(); SU.GeometryInputCreate(geometry); foreach (SU.Point3D p in points) { SU.Point3D pc = p; pc.x *= SU.MetersToInches; pc.y *= SU.MetersToInches; pc.z *= SU.MetersToInches; SU.GeometryInputAddVertex(geometry, pc); } SU.LoopInputRef loop = new SU.LoopInputRef(); SU.LoopInputCreate(loop); SU.LoopInputAddVertexIndex(loop, 0); SU.LoopInputAddVertexIndex(loop, 1); SU.LoopInputAddVertexIndex(loop, 2); SU.LoopInputAddVertexIndex(loop, 3); long faceIndex; SU.GeometryInputAddFace(geometry, loop, out faceIndex); SU.MaterialRef material = new SU.MaterialRef(); SU.MaterialCreate(material); SU.MaterialSetName(material, "Placeholder"); SU.TextureRef texture = new SU.TextureRef(); SU.TextureCreateFromFile( texture, "PlaceHolderRGBY.png", SU.MetersToInches, SU.MetersToInches); SU.MaterialSetTexture(material, texture); SU.MaterialInput materialInput = new SU.MaterialInput(); materialInput.materialRef = material; materialInput.numUVCoords = 4; materialInput.materialRef = material; materialInput.SetUVCoords( new SU.Point2D(0, 0), new SU.Point2D(1, 0), new SU.Point2D(1, .5), new SU.Point2D(0, .5)); materialInput.SetVertexIndices(0, 1, 2, 3); SU.GeometryInputFaceSetFrontMaterial(geometry, faceIndex, materialInput); loop = new SU.LoopInputRef(); SU.LoopInputCreate(loop); SU.LoopInputAddVertexIndex(loop, 3); SU.LoopInputAddVertexIndex(loop, 2); SU.LoopInputAddVertexIndex(loop, 4); SU.LoopInputAddVertexIndex(loop, 5); SU.GeometryInputAddFace(geometry, loop, out faceIndex); materialInput = new SU.MaterialInput(); materialInput.materialRef = material; materialInput.numUVCoords = 4; materialInput.materialRef = material; materialInput.SetUVCoords( new SU.Point2D(0, .5), new SU.Point2D(1, .5), new SU.Point2D(1, 1), new SU.Point2D(0, 1)); materialInput.SetVertexIndices(3, 2, 4, 5); SU.GeometryInputFaceSetFrontMaterial(geometry, faceIndex, materialInput); SU.EntitiesFill(entities, geometry, true); SUHelper.Finalize(path + @"\TwoQuadsSeamslessTexture.skp"); }
public override void Run(string path) { SU.EntitiesRef entities = SUHelper.Initialize(); // Create two Component Definitions. SU.ComponentDefinitionRef fanCD = new SU.ComponentDefinitionRef(); SU.ComponentDefinitionCreate(fanCD); SU.ComponentDefinitionSetName(fanCD, "Fan"); SU.ComponentDefinitionSetDescription(fanCD, "A six-bladed fan on the Y axis"); SU.ComponentDefinitionRef bladeCD = new SU.ComponentDefinitionRef(); SU.ComponentDefinitionCreate(bladeCD); SU.ComponentDefinitionSetName(bladeCD, "Fan Blade"); SU.ComponentDefinitionSetDescription(bladeCD, "Trapezoidal blade offset from axis"); // Add CDs to the model.) SU.ComponentDefinitionRef[] defs = new SU.ComponentDefinitionRef[2]; defs[0] = fanCD; defs[1] = bladeCD; SUHelper.ModelAddComponentDefinitions(defs); // The Fan CD only has instances of the blade, and no // geometry of its own. Create the blade geometry and // put it into the blade's entities. SU.GeometryInputRef bladeGeo = new SU.GeometryInputRef(); SU.GeometryInputCreate(bladeGeo); foreach (SU.Point3D p in bladePoints) { SU.Point3D pc; pc = p; pc.x *= SU.MetersToInches; pc.y *= SU.MetersToInches; pc.z *= SU.MetersToInches; SU.GeometryInputAddVertex(bladeGeo, pc); } SU.LoopInputRef loop = new SU.LoopInputRef(); SU.LoopInputCreate(loop); SU.LoopInputAddVertexIndex(loop, 0); SU.LoopInputAddVertexIndex(loop, 1); SU.LoopInputAddVertexIndex(loop, 2); SU.LoopInputAddVertexIndex(loop, 3); long faceIndex; SU.GeometryInputAddFace(bladeGeo, loop, out faceIndex); SU.EntitiesRef bladeEnts = new SU.EntitiesRef(); SU.ComponentDefinitionGetEntities(bladeCD, bladeEnts); SU.EntitiesFill(bladeEnts, bladeGeo, true); // Add six instances of the blade to the fan definition. for (int i = 0; i < 6; ++i) { AddBlade( i, fanCD, bladeCD, (15.0 / 360.0) * 2 * Math.PI, 2 * i * Math.PI / 6); } // Add one instance of the fan to the model. SU.ComponentInstanceRef fi = new SU.ComponentInstanceRef(); SU.ComponentDefinitionCreateInstance(fanCD, fi); SU.EntitiesAddInstance(entities, fi, null); SUHelper.Finalize(path + @"\SixQuadFan.skp"); }
public override void Run(string path) { SU.EntitiesRef entities = SUHelper.Initialize(); SU.GeometryInputRef geometry = new SU.GeometryInputRef(); SU.GeometryInputCreate(geometry); foreach (SU.Point3D p in parentPoints) { SU.Point3D pc = p; pc.x *= SU.MetersToInches; pc.y *= SU.MetersToInches; pc.z *= SU.MetersToInches; SU.GeometryInputAddVertex(geometry, pc); } SU.LoopInputRef loop = new SU.LoopInputRef(); SU.LoopInputCreate(loop); SU.LoopInputAddVertexIndex(loop, 0); SU.LoopInputAddVertexIndex(loop, 1); SU.LoopInputAddVertexIndex(loop, 2); SU.LoopInputAddVertexIndex(loop, 3); long faceIndex; SU.GeometryInputAddFace(geometry, loop, out faceIndex); SU.EntitiesFill(entities, geometry, false); // Now the child. SU.ComponentDefinitionRef cd = new SU.ComponentDefinitionRef(); SU.ComponentDefinitionCreate(cd); // Add CD to the model.) SU.ComponentDefinitionRef[] defs = new SU.ComponentDefinitionRef[1]; defs[0] = cd; // You leave this out and SketchUp will add the definitions to the model // itself when it opens the model. But it will prompt you on close to save // the "changes" it thinks that adding the definitions made. SUHelper.ModelAddComponentDefinitions(defs); // Get the CD's Entities. SU.EntitiesRef cdEnts = new SU.EntitiesRef(); SU.ComponentDefinitionGetEntities(cd, cdEnts); // Define a Geometry for the CD. geometry = new SU.GeometryInputRef(); SU.GeometryInputCreate(geometry); foreach (SU.Point3D p in childPoints) { SU.Point3D pc = p; pc.x *= SU.MetersToInches; pc.y *= SU.MetersToInches; pc.z *= SU.MetersToInches; SU.GeometryInputAddVertex(geometry, pc); } loop = new SU.LoopInputRef(); SU.LoopInputCreate(loop); SU.LoopInputAddVertexIndex(loop, 0); SU.LoopInputAddVertexIndex(loop, 1); SU.LoopInputAddVertexIndex(loop, 2); SU.LoopInputAddVertexIndex(loop, 3); SU.GeometryInputAddFace(geometry, loop, out faceIndex); // Fill the CD's Entities with the Geometry. SU.EntitiesFill(cdEnts, geometry, true); SU.ComponentDefinitionSetName(cd, "Quad in XY"); SU.ComponentDefinitionSetDescription(cd, "A flat square with normal on positive Z"); // Create an instance of the CD. SU.ComponentInstanceRef ci = new SU.ComponentInstanceRef(); SU.ComponentDefinitionCreateInstance(cd, ci); SU.ComponentInstanceSetName(ci, "Child Quad"); SU.EntitiesAddInstance(entities, ci, null); SUHelper.Finalize(path + @"\TwoQuadsParentChild.skp"); }
internal Group( SU.GroupRef groupRef) { // Get the transform. SU.Transformation transformation = new SU.Transformation(); SU.GroupGetTransform(groupRef, out transformation); Transform = new Transform(transformation); // Get the name. SU.StringRef stringRef = new SU.StringRef(); SU.StringCreate(stringRef); SU.GroupGetName(groupRef, stringRef); Name = Convert.ToStringAndRelease(stringRef); // Note that a Group 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 Group's material; you must // upcast first. // Upcast to a DrawingElement and get the material name. SU.DrawingElementRef drawingElementRef = SU.GroupToDrawingElement(groupRef); 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; } } // Get the entities. SU.EntitiesRef entitiesRef = new SU.EntitiesRef(); SU.GroupGetEntities(groupRef, entitiesRef); UnpackEntities(entitiesRef); }
public void WriteSketchUpFile(string path) { SU.Initialize(); SU.ModelRef modelRef = new SU.ModelRef(); SU.ModelCreate(modelRef); // Load the SketchUp structures for our materials. foreach (Material material in materials.Values) { material.Pack(modelRef); } // Load the SketchUp structures for our component definitions. foreach (CompDef componentDefinition in components.Values) { componentDefinition.Pack(this, modelRef); } // Load the SketchUp structures for our entities. SU.EntitiesRef entitiesRef = new SU.EntitiesRef(); SU.ModelGetEntities(modelRef, entitiesRef); //entities.Pack(this, entitiesRef); Pack(this, entitiesRef); // Set style and camera. SU.StylesRef stylesRef = new SU.StylesRef(); SU.ModelGetStyles(modelRef, stylesRef); SU.StylesAddStyle(stylesRef, "base.style", true); SU.CameraRef cameraRef = new SU.CameraRef(); SU.ModelGetCamera(modelRef, cameraRef); SU.CameraSetOrientation( cameraRef, new SU.Point3D( 10, // * SU.MetersToInches, -10, // * SU.MetersToInches, 10), // * SU.MetersToInches), new SU.Point3D(0, 0, 0), new SU.Vector3D(0, 0, 1)); SU.ModelSaveToFileWithVersion(modelRef, path, SU.ModelVersion_SU2017); SU.ModelRelease(modelRef); SU.Terminate(); // Mop up left-over references. foreach (Material material in materials.Values) { material.suMaterialRef = null; } foreach (CompDef componentDefinition in components.Values) { componentDefinition.componentDefinitionRef = null; } }