Access SketchUp SDK from C#/ .net project

sdk

#1

Is there a way to access the SketchUp SDK from a C# .net project with out needing to learn C or C++?


#2

Yes, with pinvoke.
See this link for more information on the subject:
https://msdn.microsoft.com/en-us/library/55d3thsc.aspx


#3

I’ll have to play around and see if I can figure it out.

If anyone would happen to have some sample code I would be forever grateful.


#4

For example how would I run the Quick start read file example from C#?

Or would it be easier to learn C++ :wink:

#include <SketchUpAPI/slapi.h>
#include <SketchUpAPI/geometry.h>
#include <SketchUpAPI/initialize.h>
#include <SketchUpAPI/unicodestring.h>
#include <SketchUpAPI/model/model.h>
#include <SketchUpAPI/model/entities.h>
#include <SketchUpAPI/model/face.h>
#include <SketchUpAPI/model/edge.h>
#include <SketchUpAPI/model/vertex.h>
#include <vector>
int main() {
  // Always initialize the API before using it
  SUInitialize();
  // Load the model from a file
  SUModelRef model = SU_INVALID;
  SUResult res = SUModelCreateFromFile(&model, "model.skp");
  // It's best to always check the return code from each SU function call.
  // Only showing this check once to keep this example short.
  if (res != SU_ERROR_NONE)
    return 1;
  // Get the entity container of the model.
  SUEntitiesRef entities = SU_INVALID;
  SUModelGetEntities(model, &entities);
  // Get all the faces from the entities object
  size_t faceCount = 0;
  SUEntitiesGetNumFaces(entities, &faceCount);
  if (faceCount > 0) {
    std::vector<SUFaceRef> faces(faceCount);
    SUEntitiesGetFaces(entities, faceCount, &faces[0], &faceCount);
    // Get all the edges in this face
    for (size_t i = 0; i < faceCount; i++) {
      size_t edgeCount = 0;
      SUFaceGetNumEdges(faces[i], &edgeCount);
      if (edgeCount > 0) {
        std::vector<SUEdgeRef> edges(edgeCount);
        SUFaceGetEdges(faces[i], edgeCount, &edges[0], &edgeCount);
        // Get the vertex positions for each edge
        for (size_t j = 0; j < edgeCount; j++) {
          SUVertexRef startVertex = SU_INVALID;
          SUVertexRef endVertex = SU_INVALID;
          SUEdgeGetStartVertex(edges[j], &startVertex);
          SUEdgeGetEndVertex(edges[j], &endVertex);
          SUPoint3D start;
          SUPoint3D end;
          SUVertexGetPosition(startVertex, &start);
          SUVertexGetPosition(endVertex, &end);
          // Now do something with the point data
        }
      }
    }
  }
  // Get model name
  SUStringRef name = SU_INVALID;
  SUStringCreate(&name);
  SUModelGetName(model, &name);
  size_t name_length = 0;
  SUStringGetUTF8Length(name, &name_length);
  char* name_utf8 = new char[name_length + 1];
  SUStringGetUTF8(name, name_length + 1, name_utf8, &name_length);
  // Now we have the name in a form we can use
  SUStringRelease(&name);
  delete []name_utf8;
  // Must release the model or there will be memory leaks
  SUModelRelease(&model);
  // Always terminate the API when done using it
  SUTerminate();
  return 0;
}


#5

First you’d have to wrap every function from the C SDK you intend to use in C#. Like this (real wrapper is 7k lines of code):

using System;

namespace YourNamespace
{
    public class API
    {
        private const string LIB = "./lib/SketchUpAPI"; # this is a dll on windows or a dylib on OSx

        [System.Runtime.InteropServices.DllImport(LIB, EntryPoint = "SUInitialize")]
        public static extern void SUInitialize();

        [System.Runtime.InteropServices.DllImport(LIB, EntryPoint = "SUTerminate")]
        public static extern void SUTerminate();

        [System.Runtime.InteropServices.DllImport(LIB, EntryPoint = "SUModelCreateFromFile")]
        public static extern SUResult SUModelCreateFromFile(ref System.IntPtr model, [System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)] string file_path);
    }
}

Once all your functions are wrapped you can call them:

API.SUInitialize();

var modelPtr = IntPtr.Zero;
var modelFilePath = "somewhere...";
API.SUModelCreateFromFile(ref modelPtr, modelFilePath);
# next is up to you...

API.SUTerminate();

#6

Great thanks for posting that. Does a C# wrapper already exist somewhere for the C SDK?


#7

Here is one … (author pushed SU2018 support only 18 days ago, so it is active.)


#8

And for at least 4 years there has been a thread on SketchUcation pointing at a tutorial …

https://sketchucation.com/forums/viewtopic.php?f=180&t=55677

the tutorial …

https://bitbucket.org/icehuli/sucsexttutorial/wiki/Home

recently updated for SU2018.