[C API] How create a polyline in SketchUp?

Dear experts,

In the SketchUp C API, I can see the following API to get a polyline. However, I don’t know how create a polyline in SketchUp. Can you help me? Thank you.

SUEntitiesGetPolyline3ds(SUEntitiesRef entities, size_t len, SUPolyline3dRef lines, size_t* count);

Did you look at the documentation for SUPolyline3dRef?

https://extensions.sketchup.com/developers/sketchup_c_api/sketchup/polyline3d_8h.html

Something like this (untested)

std::vector<SUPoint3d> points{
  {0, 0, 0},
  {3, 2, 0},
  {5, 4, 0},
  {9, 2, 0}
};

SUPolyline3dRef polyline SU_INVALID;
SUPolyline3dCreate(&polyline);
SUPolyline3dAddPoints(polyline, points.size(), points.data());

size_t count = 0;
SUEntitiesGetPolyline3ds(SUEntitiesRef entities, 1, &polyline, count);

Thank you. This is a simple way to create a polyline curve. However, it is hard to make a polyline curve in the SketchUp application manually. Even if I tried to create a polyline(attached polyline.skp), I can’t get any polyline data by the following API.

SUEntitiesGetPolyline3ds(SUEntitiesRef entities, size_t len, SUPolyline3dRef lines, size_t* count);

polyline.skp (123.0 KB)

Do you have a more complete example? Have you checked the return code?

Are your answers about the 3D polyline? (I’m not info Ruby). Are they accessible via Ruby?

I didn’t try Ruby. I have no answer how to create a 3D polyline in the SketchUp application now.

For the attached polyline.skp, I can get a curve SUCurveRef by the following API. But I can’t get a SUPolyline3dRef by SUResult res = SUEntitiesGetNumPolyline3ds(entities, &pLinecount). And res is returned SU_ERROR_NONE.
So, perhaps I used the wrong way to create a 3D polyline in the SketchUp application.

SUModelGetEntities(m_model, &entities);
size_t curveNum = 0;
SUEntitiesGetNumCurves(entities, &curveNum);
if (curveNum > 0)
{
    std::vector<SUCurveRef> curves(curveNum);
    SUEntitiesGetCurves(entities, curveNum, &curves[0], &curveNum);
}

@tt_su It seems that there is no C API to save SUPolyline3dRef into a SUModelRef.

I’m confused by your last example… you are using SUEntitiesGetNumCurves and SUEntitiesGetCurves while trying to get a SUPolyline3dRef ?

@tt_su Clarify your question:
I used SUEntitiesGetNumCurves and SUEntitiesGetCurves to be able to get a SUCurveRef from the attached polyline.skp. polyline.skp is created manually in the SketchUp application. But I can’t get a SUPolyline3dRef from the attached polyline.skp by ‘SUEntitiesGetPolyline3ds’.

So, currently, my question is: I can’t create a model containing a SUPolyline3dRef whether by C API or by the SketchUp application. For C API, I don’t know how save SUPolyline3dRef into a SUModelRef.

Maybe we are talking about different things here… Your polyline.skp doesn’t contain a SketchUp polyline *SUPolyline3dRef - it contains a Curve (SUCurveRef).

A Curve is a set of edges, in the Entities collection. From the UI you can create these with the Freehand tool. The Curve is a meta-entity - simply put it’s just a property on a set of edges that makes SketchUp treat the set of edges as a single unit.

A Polyline3d is a very different entity type - you create it from the UI by holding down Shift while using the Freehand tool. It is its own unique entity which isn’t really used. I think there’s been talks about removing it as we’re not seeing it being used. It creates a line in the model which is not geometry you can snap to or create faces from. It was probably introduced as a way to perform simple annotations.

So lets step back a bit - what is it you are looking to do? Do you really want to be dealing with SUPolyline3dRef - or are you trying to create or read curves? Can you provide some more context to what you are doing?

Btw, with your polyline.skp file I’m able to read the SUCurveRef entities in the model root:

// SLAPI-Test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <assert.h>
#include <iostream>
#include <vector>

#include "SketchUpAPI/sketchup.h"

#define SU(api_function_call) {\
  SUResult su_api_result = api_function_call;\
  assert(SU_ERROR_NONE == su_api_result);\
}\

#define refute(condition) assert(!(condition))


std::string GetString(const SUStringRef& string) {
  size_t length = 0;
  SU(SUStringGetUTF8Length(string, &length));
  std::vector<char> buffer(length + 1);
  size_t out_length = 0;
  SU(SUStringGetUTF8(string, length + 1, buffer.data(), &out_length));
  assert(out_length == length);
  return std::string(begin(buffer), end(buffer));
}


int _tmain(int argc, _TCHAR* argv[])
{
  std::string project_path = "C:\\Users\\Thomas\\SourceTree\\SLAPI-Tests";
  std::string model_path = project_path + "\\models\\polyline.skp";
  std::string temp_path = project_path + "\\temp";

  SUInitialize();

  SUModelRef model = SU_INVALID;
  //SU(SUModelCreate(&model));
  SU(SUModelCreateFromFile(&model, model_path.c_str()));

  SUEntitiesRef entities = SU_INVALID;
  SU(SUModelGetEntities(model, &entities));

  size_t num_curves = 0;
  SU(SUEntitiesGetNumCurves(entities, &num_curves));
  std::cout << "Number of curves in root: " << num_curves << "\n";
  if (num_curves > 0)
  {
    std::vector<SUCurveRef> curves(num_curves, SU_INVALID);
    SU(SUEntitiesGetCurves(entities, num_curves, curves.data(), &num_curves));

    for (const auto& curve : curves)
    {
      size_t num_edges = 0;
      SU(SUCurveGetNumEdges(curve, &num_edges));
      std::cout << "  Number of edges in curve: " << num_edges << "\n";
    }
  }


  SU(SUModelRelease(&model));

  SUTerminate();
  return 0;
}

Output:

Number of curves in root: 1
  Number of edges in curve: 4
1 Like

@tt_su Thanks you the detailed explaining. I have created a polyline3d model(attached polyine3d.skp) sucessfully by “create it from the UI by holding down Shift while using the Freehand tool”. This solved my question: I need a model containing polyline3d.

polyline3d.skp (120.0 KB)

1 Like

Out of curiosity - what are you looking to use the polyline3d for?

My intent: I am trying to find a model containing a polyline3d, and read polyline3d data by the SketchUp C API. But I don’t know how create such model containing a polyline3d curve. You recommended two ways in this thead: 1. Create a polyline3d by C API ‘SUPolyline3dCreate’ and ‘SUPolyline3dAddPoints’. 2. Create a polyline3d from the UI by holding down Shift while using the Freehand tool.

Till now, Way #2 works for me to create a model containing a polyline3d curve. Way #1 still doesn’t work for me because I don’t know how save SUPolyline3dRef into a SUModelRef . However, Way #2 has solved my issue. So, I think that we can end this thread talking by marking this issue solved. :slightly_smiling_face: Thank you for your nice support that impresses me deeply. :+1:

Hm… there is no SUEntitiesAddPolyline3ds… how bizarre that the API to create the object has been done, but not any methods to add to the model… :thinking: I’ll log a ticket for that.

Still curious to why… You say to find a model containing a polyline3d - in what context would you find one? Who are using Polyline3d entities, and what are they using it for?