How to: Read or set datafield


Topic:

Read or write datafield


Class hierarchy:

IPCsApplication IPCsDocument IPCsPage IPCsSymbol IPCsDatafield


Description:

When writing, add a datafield to a symbol, and then select the information it should be showing
When reading, just go through the symbols datafields items, and get the text from the property DisplayText.


Sourcecode:
[+] Delphi
uses DPSCAD_TLB;

var
  MyPoint : TPCsPoint3D;
  ALibtype : IPCsLibType;
  ADatafield : IPCsDatafield;
  ADataFields : IPCsDatafields;
  ASymbol : IPCsSymbol;
  i, t : integer;
  APage : IPCspage;
  AStr : string;
begin
  {write datafield value}

  {adding a symbol to the LibType}
  ALibtype := PCsApplication.ActiveDocument.Drawing.LibTypes.Add('SymbolFileName.sym');
  {MyPoint is the offset, from the symbols position. So this means that the
      datafield will be placed 10000 up, and 10000 right, from the symbols position}

  MyPoint.X := 10000;
  MyPoint.Y := 10000;
  MyPoint.Z := 0;
  {add a datafield to the libtype}
  ADatafield := ALibtype.Datafields.Add(MyPoint,'');
  {process the datafield}
  with ADatafield do
    begin
      {setting the pretext}
      Pretext := 'My Pretext';
      {ProjectData (none-predefined) see list here}
      SetDataNo(10 {FirstProjData},PCsApplication.ActiveDocument.DataLink);
      FieldName := 'Customer Name'; {to get a list of valid valued, see this code}
    end;
  {getting current page}
  APage := PCsApplication.ActiveDocument.ActivePage;
  {create a symbol, from LibType}
  ASymbol := APage.Symbols.Add(ALibtype);
  {setting symbolname}
  ASymbol.FullName := 'MyNewSymbol';
  {setting position}
  ASymbol.SetPosition(100000, 100000,0);
  {repaint project}
  PCsApplication.Redraw;



  {read a datafield value}

  {get active page}
  APage := PCsApplication.ActiveDocument.ActivePage;
  if APage.Symbols.Count > 0 then
    for i := 0 to APage.Symbols.Count - 1 do {for each symbol}
      if APage.Symbols.Item[i].Datafields.Count > 0 then {symbols has datafields}
        begin
          AStr := '';
          {get the symbols datafields}
          ADataFields := APage.Symbols.Item[i].Datafields;
          for t := 0 to ADataFields.Count - 1 do
            if ADatafields.Item[t].FieldName <> '' then
              begin
                {none predefined value}
                AStr := AStr + ADatafields.Item[t].FieldName + ' = ' + ADatafields.Item[t].DisplayText + #13+#10
              end;
            else
              begin
                {predefined value}
                {the number of the field will be shown, not the fieldname. See the list here to convert}
                AStr := AStr + IntToStr(ADatafields.Item[t].DataNo) + ' = ' + ADatafields.Item[t].DisplayText + #13+#10;
              end;
            ShowMessage('SymbolName: ' + APage.Symbols.Item[i].FullName + #13#10 + AStr);
        end;
end;

[+] Visual Basic
Dim libType As Dpscad.PCsLibType
Dim point As Dpscad.TPCsPoint3D
Dim dataFields As Dpscad.PCsDatafields
Dim dataField As Dpscad.PCsDatafield
Dim page As Dpscad.PCsPage
Dim symbol As Dpscad.PCsSymbol
Dim str As String

'write datafield value

'adding a symbol to the LibType
libType = PCsApplication.ActiveDocument.Drawing.LibTypes.Add("SymbolFileName.SYM")
'MyPoint is the offset, from the symbols position. So this means that the
'datafield will be placed 10000 up, and 10000 right, from the symbols position
point.X = 10000
point.Y = 10000
point.Z = 0
'add a datafield to the libtype
dataField = libType.Datafields.Add(point, "")
'process the datafield
'setting the pretext
dataField.Pretext = "My Pretext"
'ProjectData (none-predefined)
dataField.SetDataNo(10, PCsApplication.ActiveDocument.DataLink)
dataField.FieldName = "Customer Name" 'to get a list of valid valued, see this code

'ProjectData (predefined) see the list here
dataField.SetDataNo(17, PCsApplication.ActiveDocument.DataLink)
'getting current page
page = PCsApplication.ActiveDocument.ActivePage
'create a symbol, from LibType
symbol = page.Symbols.Add(libType)
'setting symbolname
symbol.FullName = "MyNewSymbol"
'setting position
symbol.SetPosition(100000, 100000, 0)
'repaint projec/
PCsApplication.Redraw()


'read a datafield value

'get active page
page = PCsApplication.ActiveDocument.ActivePage
If page.Symbols.Count > 0 Then
  For i As Integer = 0 To page.Symbols.Count - 1 'for each symbol
    If page.Symbols(i).Datafields.Count > 0 Then 'symbols has datafields
      str = ""
      'get the symbols datafields
      dataFields = page.Symbols(i).Datafields
      For j As Integer = 0 To dataFields.Count - 1
        If dataFields(j).FieldName <> "" Then
          'none predefined value
          str += (dataFields(j).FieldName & " = ") + dataFields(j).DisplayText & vbLf
        Else
          'predefined value
          'the number of the field will be shown, not the fieldname. See the list here to convert
          str += (dataFields(j).DataNo & " = ") + dataFields(j).DisplayText & vbLf
        End If
      Next
      MessageBox.Show(("SymbolName: " & page.Symbols(i).FullName & vbLf) + str)
    End If
  Next
End If

[+] C#
Dpscad.PCsLibType libType;
Dpscad.TPCsPoint3D point;
Dpscad.PCsDatafields dataFields;
Dpscad.PCsDatafield dataField;
Dpscad.PCsPage page;
Dpscad.PCsSymbol symbol;
string str;

/*write datafield value*/

/*adding a symbol to the LibType*/
libType = PCsApplication.ActiveDocument.Drawing.LibTypes.Add("SymbolFileName.SYM");
/*MyPoint is the offset, from the symbols position. So this means that the*/
  datafield will be placed 10000 up, and 10000 right, from the symbols position*/
point.X = 10000;
point.Y = 10000;
point.Z = 0;
/*add a datafield to the libtype*/
dataField = libType.Datafields.Add(point, "");
/*process the datafield*/
/*setting the pretext*/
dataField.Pretext = "My Pretext";
/*ProjectData (none-predefined)*/
dataField.SetDataNo(10 /*FirstProjData*/, PCsApplication.ActiveDocument.DataLink);
dataField.FieldName = "Customer Name"; /*to get a list of valid valued, see this code*/

/*ProjectData (predefined) see the list here*/
dataField.SetDataNo(17 /*RemarksField*/, PCsApplication.ActiveDocument.DataLink);
/*getting current page*/
page = PCsApplication.ActiveDocument.ActivePage;
/*create a symbol, from LibType*/
symbol = page.Symbols.Add(libType);
/*setting symbolname*/
symbol.FullName = "MyNewSymbol";
/*setting position*/
symbol.SetPosition(100000, 100000, 0);
/*repaint project*/
PCsApplication.Redraw();


/*read a datafield value*/

/*get active page*/
page = PCsApplication.ActiveDocument.ActivePage;
if (page.Symbols.Count > 0)
  for (int i = 0; i < page.Symbols.Count; i++) /*for each symbol*/
    if (page.Symbols[i].Datafields.Count > 0) /*symbols has datafields*/
    {
      str = "";
      /*get the symbols datafields*/
      dataFields = page.Symbols[i].Datafields;
      for (int j = 0; j < dataFields.Count; j++)
        if (dataFields[j].FieldName != "")
        {
          /*none predefined value*/
          str += dataFields[j].FieldName + " = " + dataFields[j].DisplayText + "\n";
        }
        else
        {
          /*predefined value*/
          /*the number of the field will be shown, not the fieldname. See the list here to convert*/
          str += dataFields[j].DataNo + " = " + dataFields[j].DisplayText + "\n";
        }
      MessageBox.Show("SymbolName: " + page.Symbols[i].FullName + "\n" + str);
    }