Welcome
Collapse Getting StartedGetting Started
Overview
Expand Types of SOLIDWORKS API ApplicationsTypes of SOLIDWORKS API Applications
Expand SOLIDWORKS API Object Model and Class HierarchySOLIDWORKS API Object Model and Class Hierarchy
Collapse Programming with the SOLIDWORKS APIProgramming with the SOLIDWORKS API
Expand Add-insAdd-ins
API Dependent on SOLIDWORKS Being Visible
Arrays
Attributes Imported from ACIS SAT Files
Bitmasks
Block Definitions and Block Instances
Bodies in Body Folders
BOOL and VARIANT_BOOL
Bounding Box and Note Text
COM vs. Dispatch
CommandManager and CommandGroups
Components, Configurations, and Suppression States
Compound Note
ComVisibleAttribute in in VB.NET and C# Macros and Add-ins
Controls, Visual Basic 6.0, and Windows XP
Drawing Views and Model Entities
Early and Late Binding
Expand EventsEvents
Features of Components
Helper Functions
Hole Wizard Features and Objects
IDispatch Object Arrays as Input in .NET
Implementation Guidelines
Import and Export File Options
In-process Methods
Instantiate ActiveX Controls as Tabs
Interface Pointers
ISafeArrayUtility Interface Overview
Keystrokes and Accelerator Keys
Library Features and Objects
Lightweight Components
Line Attributes for View::GetPolyLinesAndCurves
Line Weights
Long vs. Integer
Expand Macro FeaturesMacro Features
Manipulators
Mass Properties
Expand Multibody PartsMultibody Parts
Option Explicit Statement
Collapse Packing and Unpacking Double Arrays and Integer PairsPacking and Unpacking Double Arrays and Integer Pairs
VB.NET and VBA
C#
C++
Partition Rollback and API Handles
Persistent Reference IDs
Presentation Transforms
Expand PropertyManager PagesPropertyManager Pages
.NET Interop Assemblies
Quick Tips and Bubble ToolTips
Return Values
Expand SafeArraysSafeArrays
Selection Criteria
Selection Lists
Selections that Define Features
Setup Project to Distribute SOLIDWORKS Add-in
Smart Pointers
SOLIDWORKS Objects
Sorting Tables
Splines
SQLite
STL Container Classes and Smart Pointers
Suspend Automatic Rebuilds
System Options and Document Properties
Tessellation and Edges
Third-party Data in SOLIDWORKS Files
Tracking IDs
Expand Type LibrariesType Libraries
Units
Unmanaged C++ and C++/CLI Code
VBA and SOLIDWORKS x64
Examples and Projects
Expand SOLIDWORKS API HelpSOLIDWORKS API Help
Expand SOLIDWORKS Enterprise PDM API HelpSOLIDWORKS Enterprise PDM API Help
Expand FeatureWorks API HelpFeatureWorks API Help
Expand SOLIDWORKS Costing API HelpSOLIDWORKS Costing API Help
Expand SOLIDWORKS Document Manager API HelpSOLIDWORKS Document Manager API Help
Expand SOLIDWORKS Routing API HelpSOLIDWORKS Routing API Help
Expand SOLIDWORKS Simulation API HelpSOLIDWORKS Simulation API Help
Expand SOLIDWORKS Sustainability API HelpSOLIDWORKS Sustainability API Help
Expand SOLIDWORKS Toolbox API HelpSOLIDWORKS Toolbox API Help
Expand SOLIDWORKS Utilities API HelpSOLIDWORKS Utilities API Help
Expand SOLIDWORKS Workgroup PDM API HelpSOLIDWORKS Workgroup PDM API Help
Expand eDrawings API HelpeDrawings API Help
Hide Table of Contents

Unpacking and Packing Arrays in C#

Some of the arguments passed from and to SOLIDWORKS using the API contain arrays of doubles. In some functions, elements in these arrays contain two integers packed into a double array element. You can unpack the data from a double to two integers and vice versa.

 

using System;

using System.Runtime.InteropServices;

namespace WindowsApplication2

{

[StructLayout(LayoutKind.Explicit)]

public struct DoubleIntConv

{

// An 8-byte double contains 2 4-byte ints.

[FieldOffset(0)] private int    m_Int1;

[FieldOffset(4)] private int    m_Int2;

[FieldOffset(0)] private double m_Double;

 

private DoubleIntConv(double dValue)

{

//C# wants these initialized in the constructor

m_Int1 = 0; m_Int2 = 0;

 

m_Double = dValue;

}

 

private DoubleIntConv(int iValue1, int iValue2)

{

//C# wants this initialized in the constructor

m_Double = 0.0;

 

m_Int1 = iValue1; m_Int2 = iValue2;

}

 

// Use  out parameters, so client code can pass in an uninitialized variable

//Unpack

public static void Unpack (double dIn, out int iOut1, out int iOut2)

{

DoubleIntConv   cv;

 

cv = new DoubleIntConv(dIn);

 

iOut1 = cv.m_Int1;

iOut2 = cv.m_Int2;

 

return;

}

 

//Use an out parameter, so client code can pass in an uninitialized variable

//Pack

public static void Pack (int iIn1, int iIn2, out double dOut)

{

DoubleIntConv   cv;

 

cv = new DoubleIntConv(iIn1, iIn2);

 

dOut = cv.m_Double;

 

return;

}

}

 

class Program

{

[STAThread]

static void Main()

{

 

int             iValueIn1  = 65535;

int             iValueIn2  = 0;

double          dValueOut;

int             iValueOut1;

int             iValueOut2;

 

DoubleIntConv.Pack(iValueIn1, iValueIn2, out dValueOut);

 

DoubleIntConv.Unpack(dValueOut, out iValueOut1, out iValueOut2);

 

return;

}

}

}

See Create Reference Curve Example (C#) for an example of packing an array.



Provide feedback on this topic

SOLIDWORKS welcomes your feedback concerning the presentation, accuracy, and thoroughness of the documentation. Use the form below to send your comments and suggestions about this topic directly to our documentation team. The documentation team cannot answer technical support questions. Click here for information about technical support.

* Required

 
*Email:  
Subject:   Feedback on Help Topics
Page:   Unpacking and Packing Arrays in C#
*Comment:  
*   I acknowledge I have read and I hereby accept the privacy policy under which my Personal Data will be used by Dassault Systèmes

Print Topic

Select the scope of content to print:



x

We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

 Never show this message again
x

Web Help Content Version: API Help (English only) 2015 SP05

To disable Web help from within SOLIDWORKS and use local help instead, click Help > Use SOLIDWORKS Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.