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);
        }
Beispiel #2
0
        void UnpackMaterials(SU.ModelRef modelRef)
        {
            SU.ModelGetNumMaterials(modelRef, out long count);

            SU.MaterialRef[] materialRefs = new SU.MaterialRef[count];

            long len = count;

            SU.ModelGetMaterials(modelRef, len, materialRefs, out count);

            foreach (SU.MaterialRef materialRef in materialRefs)
            {
                Material material = new Material(materialRef);

                materials.Add(material.Name, material);
            }
        }
        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);
        }
        void UnpackComponents(SU.ModelRef modelRef)
        {
            SU.ModelGetNumComponentDefinitions(modelRef, out long count);

            SU.ComponentDefinitionRef[] componentDefinitionRefs = new SU.ComponentDefinitionRef[count];

            long len = count;

            SU.ModelGetComponentDefinitions(modelRef, len, componentDefinitionRefs, out count);

            foreach (SU.ComponentDefinitionRef componentDefinitionRef in componentDefinitionRefs)
            {
                CompDef component =
                    new CompDef(componentDefinitionRef);

                components.Add(component.Name, component);
            }
        }
        internal void Pack(SU.ModelRef suModelRef)
        {
            suMaterialRef = new SU.MaterialRef();
            SU.MaterialCreate(suMaterialRef);

            SU.MaterialSetName(suMaterialRef, Name);

            switch (suMaterialType)
            {
            case SU.MaterialType_Colored:

                SU.MaterialSetColor(suMaterialRef, Color.SUColor);

                break;

            case SU.MaterialType_Textured:

                Texture.Pack();
                SU.MaterialSetTexture(suMaterialRef, Texture.textureRef);

                break;

            case SU.MaterialType_ColorizedTexture:

                SU.MaterialSetColor(suMaterialRef, Color.SUColor);
                SU.MaterialSetColorizeType(suMaterialRef, suMaterialColorizeType);

                Texture.Pack();
                SU.MaterialSetTexture(suMaterialRef, Texture.textureRef);

                break;

            default:
                throw new Exception($"Unknown material type = {suMaterialType}");
            }

            SU.MaterialRef[] suMaterialRefs = new SU.MaterialRef[1];

            suMaterialRefs[0] = suMaterialRef;

            SU.ModelAddMaterials(suModelRef, 1, suMaterialRefs);
        }
Beispiel #6
0
        /// <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();
        }
Beispiel #7
0
        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;
            }
        }