HOW TO

Create a PolyLine in ArcObjects using C++

Last Published: April 25, 2020

Summary

This example illustrates how to use ArcObjects with C++ to create a polyline feature programmatically.

Procedure

This article assumes you have previous programming experience with ArcObjects and C++ and are familiar with the ArcMap Editor.


Code:
HRESULT loadRandomData(IFeatureClass * pFeatureClass, long numPts)
{

// start editing on the workspace
IDatasetPtr ipDataset(pFeatureClass);
IWorkspacePtr ipWksp;
ipDataset->get_Workspace(&ipWksp);
IWorkspaceEditPtr ipWkspEdit(ipWksp);

ipWkspEdit->StartEditing(VARIANT_TRUE);
ipWkspEdit->StartEditOperation();

// Add rows using an insert cursor vs. create feature..
IFeatureCursorPtr ipFeatureCursor;
if (FAILED(pFeatureClass->Insert(VARIANT_TRUE,
&ipFeatureCursor)))
{
ipWkspEdit->AbortEditOperation();
ipWkspEdit->StopEditing(VARIANT_FALSE);
return E_FAIL;
}

// seed the random number generator..
srand( (unsigned)time( NULL ) );
CComVariant varID;

IPolylinePtr ipLine(CLSID_Polyline);
IPointCollectionPtr ipPoints(ipLine);

IPointPtr ipPoint;
IFeatureBufferPtr ipFeatureBuffer;
double x, y;
for(long i=0;i<numPts;i++)
{
if
(FAILED(pFeatureClass->CreateFeatureBuffer(&ipFeatureBuffer)))
continue;

ipPoint.CreateInstance(CLSID_Point);
x = (-180 * ((double)rand()/RAND_MAX)) + 1;
y = (180 * ((double)rand()/RAND_MAX)) + 1;
ipPoint->put_X(x);
ipPoint->put_Y(y);
ipPoints->AddPoint(ipPoint, 0, 0);

// ipFeatureBuffer->putref_Shape((IGeometryPtr)ipPoint);
// ipFeatureCursor->InsertFeature(ipFeatureBuffer, &varID);
}

// Update the field values now
long iTextField, iLongField;
ipFeatureBuffer->putref_Shape((IGeometryPtr)ipLine);
pFeatureClass->FindField(CComBSTR("TextField"), &iTextField);
pFeatureClass->FindField(CComBSTR("LongField"), &iLongField);
ipFeatureBuffer->put_Value(iTextField,
CComVariant(_T("SomeText")));
ipFeatureBuffer->put_Value(iLongField, CComVariant(1000));

ipFeatureCursor->InsertFeature(ipFeatureBuffer, &varID);

ipWkspEdit->StopEditOperation();
ipWkspEdit->StopEditing(VARIANT_TRUE);

return S_OK;
}


Article ID: 000004592

Software:
  • ArcMap 8 x
  • ArcMap 9 x

Receive notifications and find solutions for new or common issues

Get summarized answers and video solutions from our new AI chatbot.

Download the Esri Support App

Discover more on this topic

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options