⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.pas.~941~

📁 自动化控制visio
💻 ~941~
📖 第 1 页 / 共 2 页
字号:
  VisioMaster: Master;
  VisioMasters: Masters;
  mastersDoc: Document;
  connectShape: Shape;
  pinCell, begCell, endCell: Cell;
  vsoCell: Cell;
  x, y, x1, y1: Integer;
  pt: TPoint;
  d: Double;
  str: string;
begin
  Memo1.Lines.Clear;
  try
    VisioApp := CoVisioApplication.Create;
    VisioApp.Settings.ShowChooseDrawingTypePane := False;
    VisioApp.Visible := True;
      Button2.Enabled := True;
      Button3.Enabled := True;
      Button4.Enabled := True;
      Button6.Enabled := True;
    
    visioDoc := VisioApp.Documents.Add('');  //新建绘图
    // BASFLO_M.vss ---基本流程图模板
    // 打开模具模板
    mastersDoc := visioDoc.Application.Documents.OpenEx('BASFLO_M.vss', visOpenDocked);
    //mastersDoc := VisioApp.Documents.OpenEx('BASFLO_M.vss', visOpenCopy); //另新建窗口显示了
    VisioMasters := mastersDoc.Masters;
    {
    Memo1.Lines.Add(VisioMasters.ItemFromID[0].NameU);
    I := 2;
    repeat
      with Memo1.Lines do
      begin
        Add(VisioMasters.ItemFromID[I].NameU);
        //Add(IntToStr(VisioMasters.get_ItemU('Document').ID));
      end;
      Inc(I);
    until I > VisioMasters.Count;
    }
    //VisioMaster := VisioMasters.ItemFromID[0];   //按ID号取得模具图形
    VisioMaster := VisioMasters.get_ItemU('Process');   //按NameU取得模具图形

    visioPage := visioDoc.Pages[1];
    visioshapes := visioPage.Shapes;


    x := XScreenToDrawing(VisioApp.ActiveWindow, 400, 400);
    y := YScreenToDrawing(VisioApp.ActiveWindow, 400, 400);
    visioPage.Drop(VisioMaster, x, y);
    visioPage.Drop(VisioMaster, 2, 3);
//    visioShape1 := visPgDrop(visioPage, VisioMaster, x, y);
//    visioShape1.Text := '开始';
//    visioShape1.Cells['Width'].Result['mm'] := 20;
//    visioshape1.Cells['Height'].Result['mm'] := 50;
//    visioapp.ActiveWindow.DeselectAll;
//    visioapp.ActiveWindow.Select(visioshape1, visselect);
//
//    x := XScreenToDrawing(VisioApp.ActiveWindow, 400, 400);
//    y := YScreenToDrawing(VisioApp.ActiveWindow, 400, 400);
//    visioShape := visPgDrop(visioPage, VisioMaster, x, y);
//    visioShape.Text := '结束';
//    visioShape.Cells['Width'].Result['mm'] := 20;
//    visioshape.Cells['Height'].Result['mm'] := 50;

//    visioapp.ActiveWindow.Select(visioshape, visselect);
//    visioapp.ActiveWindow.Selection.ConnectShapes;

//    pt := XYDrawingToScreen(VisioApp.ActiveWindow, 2, 2);
//    Memo1.Lines.Add('x:' + IntToStr(pt.x) + ';Y:' + IntToStr(pt.Y));
//    str := visioShape.TextStyle;
//    Memo1.Lines.Add(str);
//    str := IntToStr(visioShape.Get_type_);
//    memo1.Lines.Add(str);

//    x := XScreenToDrawing(VisioApp.ActiveWindow, 500, 300);
//    y := YScreenToDrawing(VisioApp.ActiveWindow, 500, 300);
//    visPgDrop(visioPage, VisioMaster, x, y);

//    //取得属性cell,设置属性值
//    vsoCell := visioShape.get_CellsSRC(visSectionProp, 2, visCustPropsValue);
//    vsoCell.Set_Formula('1231231');
//    //设置属性名称
//    vsoCell := visioShape.get_CellsSRC(visSectionProp, 1, visCustPropsLabel);
//    vsoCell.Set_RowNameU('chengben');
    //visioShape.AddSection(visSectionScratch);
    {
    vsoSection := visioShape.Section[1];
    visioShape.AddRow(visSectionLastComponent, 3, 0);
    vsoCell := visioShape.get_CellsSRC(visSectionProp, 3, visCustPropsLabel);
    vsoCell.Set_Formula('xinjia');
    }
    //vsoCell := visioShape.get_CellsSRC(visSectionProp, 1, visCustPropsFormat);
   // vsoCell.Set_FormulaU('成本成本');
    {
    visioShape := visioPage.DrawRectangle(1, 5, 5, 1);
    visioShape.AddSection(visSectionScratch);
    visioshape.AddRow(visSectionScratch, visRowScratch, 0);
    }
    {
    vsoCell := visioShape.Cells['Scratch.X1'];
    vsoCell.Formula := 'Min(Width, Height) / 5';
     }
//    VisioMaster := VisioMasters.get_ItemU('Dynamic connector');
//    connectShape := visPgDrop(visioPage, VisioMaster, 5, 5); //放连接线


//    str := IntToStr(connectShape.Get_type_);
//    memo1.Lines.Add(str);
//    str := connectShape.Style;
//    memo1.Lines.Add(str);
//    str := connectShape.LineStyle;
//    memo1.Lines.Add(str);
//    x1 := XScreenToDrawing(VisioApp.ActiveWindow, 20, 50);
//    y1 := YScreenToDrawing(VisioApp.ActiveWindow, 20, 50);

//    VisioMaster := VisioMasters.get_ItemU('Dynamic connector');
//    connectShape := visPgDrop(visioPage, VisioMaster, 1, 1); //放连接线
//    //connectShape.LineStyle :='Guide';
//    connectshape.SetBegin(3, 3);
//    connectShape.CellsSRC[visSectionObject,visRowShapeLayout,visSLOLineRouteExt].FormulaU := IntToStr(1);
//    connectshape.CellsSRC[visSectionObject,visRowShapeLayout,visSLORouteStyle].FormulaU := inttostr(16);
//    str := connectShape.CellsSRC[visSectionObject,visRowShapeLayout,visSLOLineRouteExt].FormulaU;
//    str := connectShape.CellsU['ConLineRouteExt'].ResultStr[''];
//    memo1.Lines.add(str);

//    visioShape := visioShapes.ItemU['Process'];
//    pinCell := visioShape.Cells['Connections.X2'];  //获得shape的连接点
//    begCell := connectShape.Cells['BeginX'];        //获得连接线的起始点
//    begCell.GlueTo(pinCell);                        //连接

    //x = shape.get_CellsSRC((short)visSectionObject, visRowXFormOut, (short)VisCellIndices.visXFormWidth).ResultIU;
  //  y = shape.get_CellsSRC((short)visSectionObject, (short)visRowXFormOut, (short)VisCellIndices.visXFormHeight).ResultIU;
//
//    VisioMaster := VisioMasters.get_ItemU('Process');
//    visioShape :=  visPgDrop(visioPage, VisioMaster, 5, 5);
//    visioShape.Text := '结束';
//    pinCell := visioShape.Cells['Connections.X1'];
//    endCell := connectShape.Cells['EndX'];
//    endCell.GlueTo(pinCell);
    //visioPage.DrawRectangle(1,2,2,1);
    //visPgDrop(visioPage, visioShape, 3, 3);

   // visioShape.CellsU[''].
//    Memo1.Lines.Add(visioShape.Name);
//    Memo1.Lines.Add(visioShape.NameU);
    visioDoc.SaveAsEx(ExtractFilePath(Application.ExeName) + 'test.vsd', 0);
  except
     VisioApp.Quit;
      Button2.Enabled := False;
      Button3.Enabled := False;
      Button4.Enabled := False;
      Button6.Enabled := False;
  end;

end;

function GetName(str: string): string ;
var
  startPos : Integer;
begin
  startPos := pos('.', str);
          if (startPos > 0) then
            Result := LeftStr(str, startPos-1)
          else result := str;
end;

function GetXI(str: string): Integer;
var
  startPos : Integer;
begin
  startPos := pos('.', str);
          if (startPos > 0) then
            Result := StrToInt(MidStr(str, startPos+1, Length(str)));
end;

procedure GetGluePos(Xindex: Integer; height, width: Double;
               var x: Double; var y: Double);
begin
   case Xindex of
    1: begin
        x := x - width / 2;
        y := y;
       end;
    2: begin
        x := x + width / 2;
        y := y;
       end;
    3: begin
        x := x;
        y := y + height / 2;
       end;
    4: begin
        x := x;
        y := y - height / 2;
       end;
   end;
end;

procedure GetLineEndPos(height, width: Double; var x: Double; var y: Double);
begin
  x := x + width;
  y := y + height;
end;

procedure TForm1.GetCode(ToSheet: Shape; FromSheetName: string = '');
var
  I, J, Xindex: Integer;
  lineOnshapeConns, shapeOnLineConns: Connects;
  visFromSheet, visToSheet: Shape;
  graph: TGraphRec;
  filename: TextFile;
  x, y, x1, y1: Double;
  endCell: Cell;
  str: string;
begin
  AssignFile(filename, 'graph.txt');
  try
    Reset(filename);
  lineOnshapeConns := ToSheet.FromConnects;  //获取FromConnects集合 实际是连接线的集合
  for J := 1 to lineOnshapeConns.Count do
  begin
    visFromSheet := lineOnshapeConns[J].FromSheet;   //从FromConnects集合中获取连接线
    if visFromSheet.Name <> FromSheetName then
    begin
      shapeOnLineConns := visFromSheet.Connects;   //获取连接线的Connects集合 实际是连接的图形的集合
      for I := 1 to shapeOnLineConns.Count do
      begin
        visToSheet := shapeOnLineConns[I].ToSheet;    //获取连接线连接的图形
        if (visToSheet.NameU <> ToSheet.NameU) and (GetName(visToSheet.NameU) = 'Process') then
        begin
          Memo1.Lines.Add(ToSheet.Text + '  ' +
                            visFromSheet.Name + '  ' +
                             visFromSheet.Text + '  then  ' +
                              visToSheet.Text);
          //Seek(filename, FileSize(filename));



          x := ToSheet.CellsU['PinX'].Result['mm'];  //实际取得的是中间点的坐标
          y := ToSheet.CellsU['PinY'].Result['mm'];
          //ToSheet.XYFromPage(0, 0, x, y);
          graph.height := ToSheet.CellsU['Height'].Result['mm']; //进程的宽和高
          graph.width := ToSheet.CellsU['Width'].Result['mm'];
          graph.left := x - graph.width / 2;  //还需要坐标变换
          graph.top := y + graph.height / 2;
          ToSheet.XYFromPage(x, y, x1, y1);
          graph.text := ToSheet.Text;
          str := lineOnshapeConns[J].ToCell.Name;
          Xindex := GetXI(str);
          GetGluePos(Xindex, graph.height, graph.width, x, y);//取得连接点的位置
//          x := lineOnshapeConns[J].ToCell.Cells['PinX'].Result['mm'];
//          y := lineOnshapeConns[J].ToCell.CellsU['PinX'].Result['mm']; //连接点
//          x1 := visFromSheet.CellsU['PinX'].Result['mm'];
//          y1 := visFromSheet.CellsU['PinY'].Result['mm'];
          graph.height := visFromSheet.CellsU['Height'].Result['mm'];//连接线的宽和高
          graph.width := visFromSheet.CellsU['Width'].Result['mm'];
          GetLineEndPos(graph.height, graph.width, x, y);
          visfromsheet.XYFromPage(x, y, x1, y1);
          //str := visFromsheet.Name;
//          x := visFromSheet.CellsU['EndX'].Result['mm'];
//          y := visFromSheet.CellsU['EndY'].Result['mm'];
          GetCode(visToSheet, visFromSheet.Name);   //递归调用
        end;
        if visToSheet = nil then Continue;
      end;
    end;
  end;
  finally
    CloseFile(filename);
  end;
end;

procedure TForm1.GetSubShapes(ASubsysName: string);
var
  I: Integer;
  vsoShape, LineShape: Shape;
  shapeConns: Connects;
  J: Integer;
  shapeToCell: Cell;
  vsoApp: VisioApplication;
  vsoPage: Page;
  vsoShapes: Shapes;
  vsoDoc: Document;
  PosI: Boolean;

begin
//  if CoVisioApplication = nil then
//  begin
//    ShowError('找不到Microsoft Office Visio程序!');
//    Exit;
//  end;

  try
    vsoApp := CoVisioApplication.Create;
    vsoApp.Visible := True;
    vsoApp.Settings.ShowChooseDrawingTypePane := False;
    vsoApp.Documents.Open(ExtractFilePath(Application.ExeName) + 'aaa.vsd');
//    vsoApp.Documents.Open(ExtractFilePath(Application.ExeName) + 'a.vsd');
    vsoDoc := vsoApp.Documents[1];
    vsoApp.ActiveWindow.Zoom := 1;
    vsoApp.ActiveWindow.ZoomLock := False;
    vsoPage := vsoDoc.Pages[1];
    vsoShapes := vsoPage.Shapes;
    vsoWindow := vsoApp.ActiveWindow;
    vsoSelection := vsoWindow.Selection;
//    vsoWindow.Select(vsoShape, 1);
    for I := vsoShapes.Count downto 1 do
    begin
      vsoShape := vsoShapes[I];
      PosI := AnsiStartsText('保留', vsoshape.Text);
      if PosI then
      begin
//        vsoSelection.Select(vsoShape, visSelect);
        if vsoShape.FromConnects <> nil then
        begin
          shapeConns := vsoShape.FromConnects;
          for J := 1 to shapeConns.Count do
          begin
//            ConnShape := shapeConns[J].FromSheet;
            shapeToCell := shapeConns[J].FromCell;
            if shapeToCell.Name = 'BeginX' then
            begin
              LineShape := shapeToCell.Shape;

              DeleteParents(LineShape.Connects[2].ToSheet, vsoShape);
            end;
//            if shapeToCell.Name = 'EndX' then
//            begin
//              LineShape := shapeToCell.Shape;

//              DeleteParents(LineShape.Connects[1].ToSheet, vsoShape);
//            end;
          end;
        end;
        vsoshape.Delete;
        Break;
      end;
    end;

//    vsoSelection.Group.Copy(visCopyPasteNormal);
//    vsoSelection.Copy(visCopyPasteNormal);
    vsoSelection.Cut(visCopyPasteNormal);
    vsoWindow.SelectAll;
    vsoWindow.Delete;
    vsoPage.Paste(visCopyPasteNormal);
//    vsoWindow.Copy;
//    vsoDoc.Close;


//    vsoDoc := vsoApp.ActiveDocument;
//    vsoPage.Paste(visCopyPasteNormal);
//    vsoDoc := vsoApp.Documents[2];
    vsoDoc.SaveAsEx(ExtractFilePath(Application.ExeName) + 'aaa1.vsd', 0);
  finally
    vsoApp.Quit;
  end;
end;

{
  根据流程图获取流程
}
procedure TForm1.Button6Click(Sender: TObject);
var
  I: Integer;
begin
  visioDoc := VisioApp.ActiveDocument;
  visioPage := visioDoc.Application.ActivePage;
  visioShapes := visioPage.Shapes;
  for I := 1 to visioShapes.Count do
  begin
    visioShape := visioShapes[I];
    if visioShape.Name = '进程' then
    begin
      memo1.Lines.Clear;
      Memo1.Lines.Add('开始');
      Break;
    end;
  end;
  GetCode(visioShape, visioShape.Name);
end;

procedure TForm1.Button7Click(Sender: TObject);
var
  vsoUIObject: VisioUIObject;
  vsoMenuSets: VisioMenuSets;
  vsoMenuSet: VisioMenuSet;
  vsoMenus: VisioMenus;
  vsoMenu: VisioMenu;
  vsoMenuItems: VisioMenuItems;
  vsoMenuItem: VisioMenuItem;
begin
  vsoUIObject := VisioApp.BuiltInMenus;
  vsoMenuSets := vsoUIObject.MenuSets;
  vsoMenuSet := vsoMenuSets.ItemAtID[visUIObjSetDrawing];
  vsoMenus := vsoMenuSet.Menus;
  vsoMenu := vsoMenus.AddAt(7);
  vsoMenu.Caption := 'MyNewMenu';
  vsoMenuItems := vsoMenu.MenuItems;
  vsoMenuItem := vsoMenuItems.Add;
  vsoMenuItem.Caption := '&MyNewMenuItem';
  visioDoc := VisioApp.ActiveDocument;
  visioDoc.SetCustomMenus(vsoUIObject);
end;

procedure TForm1.DeleteParents(vsoShape, PreShape: Shape);
var
  I, J: Integer;
  LineShape: Shape;
  shapeConns: Connects;
  shapeToCell: Cell;
begin
//  vsoSelection.Select(vsoShape, visSelect);
  if vsoShape.FromConnects <> nil then
  begin
    shapeConns := vsoShape.FromConnects;      //连接线的集合
    for J := 1 to shapeConns.Count do
    begin
      shapeToCell := shapeConns[J].FromCell;   //线连接的点
      if shapeToCell.Name = 'BeginX' then     //自己连出去的线
      begin
        LineShape := shapeToCell.Shape;
        //连接线两端1是自己,2是连接的图形
        Memo1.Lines.Add(vsoShape.Text);
        if LineShape.Connects[2].ToSheet.Text <> PreShape.Text then
          DeleteParents(LineShape.Connects[2].ToSheet, vsoShape);
      end;
      if shapeToCell.Name = 'EndX' then      //连接到自己的线
      begin
        LineShape := shapeToCell.Shape;
        DeleteParents(LineShape.Connects[1].ToSheet, vsoShape);
      end;
    end;
  end;
  vsoShape.Delete;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Button2.Enabled := False;
//  Button3.Enabled := False;
  Button4.Enabled := False;
  Button6.Enabled := False;
  Left := 0;
  Top := 0;
  Height := Screen.Height;
  Width := Screen.Width;
  Memo1.Height := Height - 150;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  if Assigned(VisioApp) then
  VisioApp.Quit;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -