Recent

Author Topic: different selections to image code  (Read 2378 times)

macskabenzin

  • Newbie
  • Posts: 3
different selections to image code
« on: April 24, 2018, 10:49:39 pm »
Hi,

I am a beginner in Delphi. I would like to ask the help of you.

I would like to design a program, which shows a picture according to the user's selection (To be very specific, I want to load pneumatic sketch drawings according to given pneumatic instruments. Like if you have positioner, regulator, solenoid valve, you will have a pneumatic sketch for it. Each option represents  the devices to add to the hookup). I need to have lots of options (some radiogroups, comboboxes, or there could be checkboxes).

I thought to declare a var in the beg for the image name + directory. Directory is just needed at the program to have a clean space for the drawings.

var
  Form1: TForm1;
  dir : string;
  image : integer;

//I placed a button to have an action to activate the image load... I guess this part is neccessary

procedure TForm1.Button1Click(Sender: TObject);
begin
      dir := GetCurrentDir;
      ?????????????
      image1.picture.loadfromfile(dir+'\drawings\'+inttostr(image)+'.jpg');

I think I can build up the selection what I need. My problematic part is the "????" part. I don't know how should I declare the image nb...
Like on my picture, I want to have a picture according to Option A+C+10. I am a very beginner, I can only think of using 1 million "IF" commands. Or using "case" commend, but I can't specify how exactly....

Could you give me an idea to finish my program?

Thanks!

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: different selections to image code
« Reply #1 on: April 25, 2018, 02:53:23 am »
Sounds like a retirement project  :)

But in any case what you are looking for is an electrical type CAD program or something like
LabVIEW

 The first part of the project is to make a component editor. This editor will be using a base class where all components
you drop on the sheet will have a common operation. Operations are those that locate the component on the sheet, manage the
name of it from the library, have an image index for it to display itself etc..

 This base component will have a way to have a script that describes the number of INPUT connections, what type of connection
they are, outputs etc..

 This information is needed for the viewer to be able to display the components on the sheet.

 Then you have the connecting links where these components get linked together and that is yet another series of instructions
from another script...

 I have written these types of programs, I know what is involved, you have a lot of work ahead of you  :D

 Feel free to ask questions

BTW, this is a Lazarus / Free Pascal group..
The only true wisdom is knowing you know nothing

ASerge

  • Hero Member
  • *****
  • Posts: 2222
Re: different selections to image code
« Reply #2 on: April 25, 2018, 07:26:22 am »
Like on my picture, I want to have a picture according to Option A+C+10. I am a very beginner, I can only think of using 1 million "IF" commands. Or using "case" commend, but I can't specify how exactly....
How I would do.
First, declare a separate type for each option:
Code: Pascal  [Select][+][-]
  1. type
  2.   TOption1 = (op1Nothing, op1Value1, op1Value2,...,op1ValueRezerved);
  3.   TOption2 = (op2Nothing, op2Value1, op2Value2,...,op2ValueRezerved);
  4. ...
Next, define string constants to load into the form:
Code: Pascal  [Select][+][-]
  1. const
  2.   COption1: array[TOption1] of string = ('', 'Value1', 'Value2',..., '');
  3.   COption2: array[TOption2] of string = ...
Then load them into comboboxes:
Code: Pascal  [Select][+][-]
  1. ComboBoxOp1.Items.AddStrings(COption1, True);
  2. ComboBoxOp1.ItemIndex := 0;
  3. ...
Define a function to calculate the intersection code of options:
Code: Pascal  [Select][+][-]
  1. function OptionsToCode(Op1: TOption1; Op2: TOption2;...;OpN: TOptionN): Int64;
  2. begin
  3.   Result := Ord(Op1);
  4.   Result := Result * (Ord(High(Op2)) + 1);
  5.   Inc(Result, Ord(Op2));
  6.   Result := Result * (Ord(High(Op3)) + 1);
  7.   ...
  8.   Inc(Result, Ord(OpN));
  9. end;
Then, load picture by code:
Code: Pascal  [Select][+][-]
  1. image1.picture.loadfromfile(Format('%s\drawings\%n.jpg',
  2.   [dir, OptionsToCode(TOption1(ComboBoxOp1.ItemIndex), TOption2(ComboBoxOp2.ItemIndex),...)]));
In the process, you will need the reverse function:
Code: Pascal  [Select][+][-]
  1. procedure CodeToOptions(Code: Int64; out Op1: TOption1; out Op2: TOption2;...out OpN: TOptionN);
  2. begin
  3.   OpN := TOptionN(Code mod (Ord(High(OpN)) + 1));
  4.   Code := Code div (Ord(High(OpN)) + 1);
  5.   ...
  6.   Op2 := TOption2(Code mod (Ord(High(Op2)) + 1));
  7.   Code := Code div (Ord(High(Op2)) + 1);
  8.   Op1 := TOption1(Code);
  9. end;

macskabenzin

  • Newbie
  • Posts: 3
Re: different selections to image code
« Reply #3 on: April 25, 2018, 10:45:57 pm »
Jamie,

My program doesn't needs to be that complicated. I can just turn my drawings to jpg. No need for CAD, LABview.

I appriciate your help, but I would prefer to receive a simplified example code. So I can learn it thru modifing it.
I have to admit I am quite dummy for this, I try to learn it from videos. Unfortunately I am not at this level to understand what you wrote.
But it would be so handy in the field I work, because we try to solve these programs with excel....

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: different selections to image code
« Reply #4 on: April 25, 2018, 11:02:43 pm »
Aserge has some very good options if you read his code for menus and all..

 For you I would start with a TDrawGrid component... That component allows you to draw the
contents of each cell..

 You can draw the image associated with the current location In the cell along with text and other items.
 
 having a draw grid forces you to format the display nicely like you would see in most spread sheet
programs.
 
 You can use a TImageList to hold various images that are the same size, reference this list for the image to be shown while
drawing the cells.

  These images are reference via an index so this means you can build a little script that does this.

Because  a TimageList is forced to hold all images of the same size, what I do is create different TimagelIsts that will hold
different sizes.

 Small, medium and large..

 The Script that maintains what image to show in the cell will have the Index number..

 So please experiment with the TDrawGrid and the OnDrawGrid event.

 I don't know of any open source or sample CD program written in Delphi or Lazarus at the moment.

 but one thing is for sure, you need to get off on the right foot because it can be come a huge problem if you find out
that a function you really need requires a major change of how you initiated the database structure of storing these items

get back to us when you have a little DrawGrid under your belt.

The only true wisdom is knowing you know nothing

macskabenzin

  • Newbie
  • Posts: 3
Re: different selections to image code
« Reply #5 on: April 25, 2018, 11:26:33 pm »
ASerge,

I try to simplify the program here, so I may be able to catch what you wrote here.

So in the first part, we need to specify what will we call Option 1 (like selecting the "1selection.radio" from radiogroup + "1selection.combo" from combobox)

Code: Pascal  [Select][+][-]
  1. type
  2.   TOption1 = (combobox1.itemindex=1, radiogroup1.itemindex=1);
  3.   TOption2 = (combobox1.itemindex=2, radiogroup1.itemindex=2);
  4.   TOption3 = (combobox1.itemindex=3, radiogroup1.itemindex=3);
  5.   TOption4 = (combobox1.itemindex=1);
  6.   TOption5 = (radiogroup1.itemindex=1);


And then, we defined these options to a value string. Sort of a matrix I guess.... but this part is a bit shady for me

Code: Pascal  [Select][+][-]
  1. const
  2.   COption1: array[TOption1] of string = ('1selection.combo, '1selection.radio');
  3.  COption2: array[TOption2] of string = ('2selection.combo', '2selection.radio');
  4.  COption3: array[TOption3] of string = ('3selection.combo', '3selection.radio');
  5.  COption4: array[TOption4] of string = ('1selection.combo', '');
  6.  COption5: array[TOption5] of string = ('', '1selection.radio');
  7.  

And then we implemented them to a drop down list (combobox)


Code: Pascal  [Select][+][-]
  1. ComboBoxOp1.Items.AddStrings(COption1, True);
  2.     ComboBoxOp1.ItemIndex := 0;
  3.     ComboBoxOp1.Items.AddStrings(COption2, True);
  4.     ComboBoxOp1.ItemIndex := 1;
  5.     ComboBoxOp1.Items.AddStrings(COption3, True);
  6.     ComboBoxOp1.ItemIndex := 2;
  7.     ComboBoxOp1.Items.AddStrings(COption4, True);
  8.     ComboBoxOp1.ItemIndex := 3;
  9.     ComboBoxOp1.Items.AddStrings(COption5, True);
  10.     ComboBoxOp1.ItemIndex := 4;

and then, a calculation function came in.... this is dark, i loast it from here

Code: Pascal  [Select][+][-]
  1. function OptionsToCode(Op1: TOption1; Op2: TOption2;Op3: TOption3;Op4: TOption4;Op5: TOption5): Int64;
  2.     begin
  3.       Result := Ord(Op1);
  4.       Result := Result * (Ord(High(Op2)) + 1);
  5.       Inc(Result, Ord(Op2));
  6.       Result := Result * (Ord(High(Op3)) + 1);
  7.       Result := Ord(Op3);
  8.       Result := Result * (Ord(High(Op4)) + 1);
  9.       Inc(Result, Ord(Op4));
  10.       Result := Result * (Ord(High(Op5)) + 1);
  11.      Inc(Result, Ord(Op5));
  12.       Result := Result * (Ord(High(Op6)) + 1);
  13.     end;

picture load- you load in the prev procedure

 
Code: Pascal  [Select][+][-]
  1.   image1.picture.loadfromfile(Format('%s\drawings\%n.jpg',
  2.       [dir, OptionsToCode(TOption1(ComboBoxOp1.ItemIndex), TOption2(ComboBoxOp2.ItemIndex),TOption3(ComboBoxOp3.ItemIndex),TOption4(ComboBoxOp4.ItemIndex),TOption5(ComboBoxOp5.ItemIndex));

And the reverse function

 
Code: Pascal  [Select][+][-]
  1.    procedure CodeToOptions(Code: Int64; out Op1: TOption1; out Op2: TOption2; out Op3: TOption3;out Op4: TOption4;out Op5: TOption5;);
  2.     begin
  3.       Op5 := TOption5(Code mod (Ord(High(Op5)) + 1));
  4.       Code := Code div (Ord(High(Op5)) + 1);
  5.       Op4 := TOption4(Code mod (Ord(High(Op5)) + 1));
  6.       Code := Code div (Ord(High(Op4)) + 1);
  7.       Op3 := TOption3(Code mod (Ord(High(Op5)) + 1));
  8.       Code := Code div (Ord(High(Op3)) + 1);
  9.       Op2 := TOption2(Code mod (Ord(High(Op2)) + 1));
  10.       Code := Code div (Ord(High(Op2)) + 1);
  11.       Op1 := TOption1(Code);
  12.     end;

And I guess it should look something like this: (feel free to call me retard t throw up to this :) )

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  9.   StdCtrls;
  10.  
  11. type
  12.    TOption1 = (combobox1.itemindex=1, radiogroup1.itemindex=1);
  13.   TOption2 = (combobox1.itemindex=2, radiogroup1.itemindex=2);
  14.   TOption3 = (combobox1.itemindex=3, radiogroup1.itemindex=3);
  15.   TOption4 = (combobox1.itemindex=1);
  16.   TOption5 = (radiogroup1.itemindex=1);

const
  COption1: array[TOption1] of string = ('1selection.combo, '1selection.radio');
  COption2: array[TOption2] of string = ('2selection.combo', '2selection.radio');
  COption3: array[TOption3] of string = ('3selection.combo', '3selection.radio');
  COption4: array[TOption4] of string = ('1selection.combo', '');
  COption5: array[TOption5] of string = ('', '1selection.radio');

    ComboBoxOp1.ItemIndex := 0;
    ComboBoxOp1.Items.AddStrings(COption2, True);
    ComboBoxOp1.ItemIndex := 1;
    ComboBoxOp1.Items.AddStrings(COption3, True);
    ComboBoxOp1.ItemIndex := 2;
    ComboBoxOp1.Items.AddStrings(COption4, True);
    ComboBoxOp1.ItemIndex := 3;
    ComboBoxOp1.Items.AddStrings(COption5, True);
    ComboBoxOp1.ItemIndex := 4;

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    ComboBox1: TComboBox;
    Image1: TImage;
    RadioGroup1: TRadioGroup;
    procedure Button1Click(Sender: TObject);
  private

  public

  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

function OptionsToCode(Op1: TOption1; Op2: TOption2;Op3: TOption3;Op4: TOption4;Op5: TOption5): Int64;
    begin
      Result := Ord(Op1);
      Result := Result * (Ord(High(Op2)) + 1);
      Inc(Result, Ord(Op2));
      Result := Result * (Ord(High(Op3)) + 1);
      Result := Ord(Op3);
      Result := Result * (Ord(High(Op4)) + 1);
      Inc(Result, Ord(Op4));
      Result := Result * (Ord(High(Op5)) + 1);
     Inc(Result, Ord(Op5));
      Result := Result * (Ord(High(Op6)) + 1);
    end;

procedure TForm1.Button1Click(Sender: TObject);
begin
 image1.picture.loadfromfile(Format('%s\drawings\%n.jpg',
      [dir, OptionsToCode(TOption1(ComboBoxOp1.ItemIndex), TOption2(ComboBoxOp2.ItemIndex),TOption3(ComboBoxOp3.ItemIndex),TOption4(ComboBoxOp4.ItemIndex),TOption5(ComboBoxOp5.ItemIndex));
end;

procedure CodeToOptions(Code: Int64; out Op1: TOption1; out Op2: TOption2; out Op3: TOption3;out Op4: TOption4;out Op5: TOption5;);
    begin
      Op5 := TOption5(Code mod (Ord(High(Op5)) + 1));
      Code := Code div (Ord(High(Op5)) + 1);
      Op4 := TOption4(Code mod (Ord(High(Op5)) + 1));
      Code := Code div (Ord(High(Op4)) + 1);
      Op3 := TOption3(Code mod (Ord(High(Op5)) + 1));
      Code := Code div (Ord(High(Op3)) + 1);
      Op2 := TOption2(Code mod (Ord(High(Op2)) + 1));
      Code := Code div (Ord(High(Op2)) + 1);
      Op1 := TOption1(Code);
    end;
end.   [/code]
« Last Edit: April 25, 2018, 11:29:05 pm by macskabenzin »

ASerge

  • Hero Member
  • *****
  • Posts: 2222
Re: different selections to image code
« Reply #6 on: April 26, 2018, 08:22:01 pm »
Code: Pascal  [Select][+][-]
  1. type
  2.   TOption1 = (combobox1.itemindex=1, radiogroup1.itemindex=1);
  3.   TOption2 = (combobox1.itemindex=2, radiogroup1.itemindex=2);
  4.   TOption3 = (combobox1.itemindex=3, radiogroup1.itemindex=3);
  5.   TOption4 = (combobox1.itemindex=1);
  6.   TOption5 = (radiogroup1.itemindex=1);
Read "Enumeration types" in https://www.freepascal.org/docs-html/current/ref/refsu4.html#x26-250003.1.1
It's just a descriptive name. TOptions it's common name. In your case something like:
Code: Pascal  [Select][+][-]
  1. type
  2.   TPneumaticTools = (ptImpactWrench, ptScrewdrivers, ptRivetingGuns);
  3.   TScrewdrivers = (sdStraight, sdPistol);
  4. ...

 

TinyPortal © 2005-2018