private void btnDecombileIdoc_Click(object sender, EventArgs e) { try { if (idocdb == null) { idocdb = new IdocDb(this.cbxDbConnection.Text); } else { SAPINT.Idocs.Idoc idoc = idocdb.ReadIdoc(this.txtIdocNumber.Text, this.cbxSapSystem.Text); if (idoc != null) { FormIdocMeta formIdocMeta = new FormIdocMeta(); formIdocMeta.SapSystemName = this.cbxSapSystem.Text; formIdocMeta.Idoc = idoc; formIdocMeta.IdocToTreeControl(); formIdocMeta.DtStatus = idocdb.IdocStatus; formIdocMeta.Show(); } else { MessageBox.Show("无法解析IDOC" + this.txtIdocNumber.Text); } } } catch (Exception exception) { MessageBox.Show(exception.Message); } }
/// <summary> /// 把IDOC解析成TREE CONTROL的形式。 /// </summary> public void IdocToTreeControl() { try { if (sapSystemName != null && idoc != null) { if (idocmeta == null) { idocmeta = new IdocMeta(sapSystemName); } if (idocmeta.SystemName != sapSystemName) { idocmeta = new IdocMeta(sapSystemName); } idocNew = idocmeta.DecompileIdoc(idoc); this.treeViewForIdoc.Nodes.Clear(); idocSegmentList = new Dictionary <string, IdocSegment>(); TreeNode node = new TreeNode(idocNew.DOCNUM.PadLeft(16, '0')); foreach (SAPINT.Idocs.IdocSegment item in idoc.Segments) { buildTreeNode(item, node); } this.treeViewForIdoc.Nodes.Add(node); this.treeViewForIdoc.ExpandAll(); } } catch (Exception exception) { MessageBox.Show(exception.Message); } }
/// <summary> /// 把IDOC解析成TREE CONTROL的形式。 /// </summary> public void IdocToTreeControl() { try { if (sapSystemName != null && idoc != null) { if (idocmeta==null) { idocmeta = new IdocMeta(sapSystemName); } if (idocmeta.SystemName != sapSystemName) { idocmeta = new IdocMeta(sapSystemName); } idocNew = idocmeta.DecompileIdoc(idoc); this.treeViewForIdoc.Nodes.Clear(); idocSegmentList = new Dictionary<string, IdocSegment>(); TreeNode node = new TreeNode(idocNew.DOCNUM.PadLeft(16, '0')); foreach (SAPINT.Idocs.IdocSegment item in idoc.Segments) { buildTreeNode(item, node); } this.treeViewForIdoc.Nodes.Add(node); this.treeViewForIdoc.ExpandAll(); } } catch (Exception exception) { MessageBox.Show(exception.Message); } }
/// <summary> /// 内部处理,根据IDOC类型与扩展类型,创建空的IDOC。 /// </summary> /// <param name="IdocType">IDOC类型</param> /// <param name="Enhancement">IDOC扩展类型</param> /// <param name="CreateEmpty">如果是TRUE,将不会复制标准的IDOC段</param> /// <returns></returns> private Idoc InternalCreateIdoc(string IdocType, string Enhancement, bool CreateEmpty) { des = SAPDestination.GetDesByName(m_sysName); IdocType = IdocType.ToUpper(); IRfcFunction function = des.Repository.CreateFunction("IDOCTYPE_READ_COMPLETE"); function["PI_IDOCTYP"].SetValue(IdocType); function["PI_CIMTYP"].SetValue(Enhancement); try { if (this.Logging) { // function.SaveToXML("IDOCTYPE_READ_COMPLETE_01_" + DateTime.Now.Ticks.ToString() + ".xml"); // function. } function.Invoke(des); if (this.Logging) { // function.SaveToXML("IDOCTYPE_READ_COMPLETE_02_" + DateTime.Now.Ticks.ToString() + ".xml"); } } catch (RfcAbapException exception) { if (!exception.Message.Trim().Equals("SEGMENT_UNKNOWN")) { throw new Exception("Get IDoc failed: " + exception.ToString()); } function["PI_RELEASE"].SetValue(" "); function.Invoke(des); } IRfcStructure structure = function.GetStructure("PE_HEADER"); Idoc idoc = new Idoc(IdocType, Enhancement) { Connection = this, MANDT = this.des.Client, Description = structure["DESCRP"].ToString() }; //IDOC类型的段定义 IRfcTable table = function.GetTable("PT_SEGMENTS"); Hashtable hashtable = new Hashtable(); for (int i = 0; i < table.RowCount; i++) { //一般来说,第一行都是主要的段定义。表示一个抬头定义。 if (table[i]["PARPNO"].GetValue().ToString() == "0000") { IdocSegment newSegment = new IdocSegment(); if (!CreateEmpty) { idoc.Segments.Add(newSegment); } newSegment.SegmentName = table[i]["SEGMENTTYP"].GetValue().ToString(); newSegment.SegmentType = table[i]["SEGMENTDEF"].GetValue().ToString(); newSegment.Description = table[i]["DESCRP"].GetValue().ToString(); int num2 = Convert.ToInt32(table[i]["GRP_OCCMAX"].GetValue().ToString().Substring(5, 5)); newSegment.MaxOccur = (num2 == 0) ? Convert.ToInt32(table[i]["OCCMAX"].GetValue().ToString().Substring(5, 5)) : num2; if (newSegment.MaxOccur == 0) { newSegment.MaxOccur = 1; } string key = table[i]["NR"].GetValue().ToString(); hashtable.Add(key, newSegment); } else { string str2 = table[i]["PARPNO"].GetValue().ToString(); string str3 = table[i]["NR"].GetValue().ToString(); IdocSegment segment2 = (IdocSegment)hashtable[str2]; if (segment2 == null) { throw new Exception("The idoc structure is not valid"); } IdocSegment segment3 = new IdocSegment(); if (!CreateEmpty) { segment2.ChildSegments.Add(segment3); } segment3.SegmentName = table[i]["SEGMENTTYP"].GetValue().ToString(); segment3.SegmentType = table[i]["SEGMENTDEF"].GetValue().ToString(); segment3.Description = table[i]["DESCRP"].GetValue().ToString(); int num3 = Convert.ToInt32(table[i]["GRP_OCCMAX"].GetValue().ToString().Substring(5, 5)); segment3.MaxOccur = (num3 == 0) ? Convert.ToInt32(table[i]["OCCMAX"].GetValue().ToString().Substring(5, 5)) : num3; if (segment3.MaxOccur == 0) { segment3.MaxOccur = 1; } hashtable.Add(str3, segment3); } } //查找段节点的结构定义。 IRfcTable table2 = function.GetTable("PT_FIELDS"); IDictionaryEnumerator enumerator = hashtable.GetEnumerator(); while (enumerator.MoveNext()) { IdocSegment segment4 = (IdocSegment)enumerator.Value; for (int j = 0; j < table2.RowCount; j++) { if (table2[j]["SEGMENTTYP"].GetValue().ToString().Trim() == segment4.SegmentName.Trim()) { segment4.Fields.Add(table2[j]["FIELDNAME"].GetValue().ToString(), table2[j]["DESCRP"].GetValue().ToString(), Convert.ToInt32(table2[j]["EXTLEN"].GetValue().ToString()), Convert.ToInt32(table2[j]["BYTE_FIRST"].GetValue().ToString()) - 0x40, table2[j]["DATATYPE"].GetValue().ToString(), ""); } } idoc.StoreSegmentForFurtherUse(segment4.Clone()); } return idoc; }
private static void processSingleIdoc(IRfcStructure tControl, IRfcTable datarows) { Idoc idoc = new Idoc { TABNAM = tControl["TABNAM"].GetValue().ToString().Trim(), MANDT = tControl["MANDT"].GetValue().ToString().Trim(), DOCNUM = tControl["DOCNUM"].GetValue().ToString().Trim(), DOCREL = tControl["DOCREL"].GetValue().ToString().Trim(), STATUS = tControl["STATUS"].GetValue().ToString().Trim(), DIRECT = tControl["DIRECT"].GetValue().ToString().Trim(), OUTMOD = tControl["OUTMOD"].GetValue().ToString().Trim(), EXPRSS = tControl["EXPRSS"].ToString().Trim(), IDOCTYP = tControl["IDOCTYP"].GetValue().ToString().Trim(), CIMTYP = tControl["CIMTYP"].GetValue().ToString().Trim(), MESTYP = tControl["MESTYP"].GetValue().ToString().Trim(), MESCOD = tControl["MESCOD"].GetValue().ToString().Trim(), MESFCT = tControl["MESFCT"].GetValue().ToString().Trim(), STD = tControl["STD"].GetValue().ToString().Trim(), STDVRS = tControl["STDVRS"].GetValue().ToString().Trim(), STDMES = tControl["STDMES"].GetValue().ToString().Trim(), SNDPOR = tControl["SNDPOR"].GetValue().ToString().Trim(), SNDPRT = tControl["SNDPRT"].GetValue().ToString().Trim(), SNDPFC = tControl["SNDPFC"].GetValue().ToString().Trim(), SNDPRN = tControl["SNDPRN"].GetValue().ToString().Trim(), SNDSAD = tControl["SNDSAD"].GetValue().ToString().Trim(), SNDLAD = tControl["SNDLAD"].GetValue().ToString().Trim(), RCVPOR = tControl["RCVPOR"].GetValue().ToString().Trim(), RCVPRT = tControl["RCVPRT"].GetValue().ToString().Trim(), RCVPFC = tControl["RCVPFC"].GetValue().ToString().Trim(), RCVPRN = tControl["RCVPRN"].GetValue().ToString().Trim(), RCVSAD = tControl["RCVSAD"].GetValue().ToString().Trim(), RCVLAD = tControl["RCVLAD"].GetValue().ToString().Trim(), CREDAT = tControl["CREDAT"].GetValue().ToString().Trim(), CRETIM = tControl["CRETIM"].GetValue().ToString().Trim(), REFINT = tControl["REFINT"].GetValue().ToString().Trim(), REFGRP = tControl["REFGRP"].GetValue().ToString().Trim(), REFMES = tControl["REFMES"].GetValue().ToString().Trim(), ARCKEY = tControl["ARCKEY"].GetValue().ToString().Trim(), SERIAL = tControl["SERIAL"].GetValue().ToString().Trim() }; Hashtable hashtable = new Hashtable(); for (int i = 0; i < datarows.RowCount; i++) { IRfcStructure structure = datarows[i]; if (structure["DOCNUM"].GetValue().ToString().Trim().Equals(tControl["DOCNUM"].GetValue().ToString().Trim())) { IdocSegment newSegment = new IdocSegment(); if (structure["PSGNUM"].GetValue().ToString() == "000000") { idoc.Segments.Add(newSegment); } else { IdocSegment segment2 = (IdocSegment)hashtable[structure["PSGNUM"].GetValue().ToString()]; if (segment2 != null) { segment2.ChildSegments.Add(newSegment); } else { idoc.Segments.Add(newSegment); } } newSegment.SegmentName = structure["SEGNAM"].GetValue().ToString(); string content = structure["SDATA"].GetValue().ToString(); newSegment.WriteDataBuffer(content, 0, 0x3e8); if (!hashtable.ContainsKey(structure["SEGNUM"].GetValue().ToString())) { hashtable.Add(structure["SEGNUM"].GetValue().ToString(), newSegment); } } } // IdocMeta idocMeta = new IdocMeta(ConfigFileTool.SAPGlobalSettings.GetDefaultSapCient(), idoc); // idocMeta.getIdocTypeDefinition(); // idocMeta.deCompileIdoc(); idoc.SavePlainData(@"d:/test.txt"); //idocsegment e1maram = idoc.segments["E2MARAM006", 0]; //// loop through segments and find the right ones //for (int i = 0; i < e1maram.childsegments.count; i++) // if (e1maram.childsegments[i].segmentname == "e2maktm001") // console.writeline("materialtext found: " + // e1maram.childsegments[i].readdatabuffer(4, 40)); }