Skip to content

Commit

Permalink
Merge pull request #8 from betty2310/feat_diode_cp
Browse files Browse the repository at this point in the history
feat(components): support Diode model 1N914
  • Loading branch information
betty2310 authored May 11, 2024
2 parents 3f6d6a8 + 8722c53 commit fea5f13
Show file tree
Hide file tree
Showing 11 changed files with 168 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/CustomEdge.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const turnBack = () => {
selectedOption.value = '';
let edge = findEdge(props.id)
if (edge) {
edge.data = '';
edge.data = {}
}
};
</script>
Expand Down
5 changes: 5 additions & 0 deletions src/assets/svgs/Diode.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions src/components/CircuitsListBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@
<button
class="btn"
:draggable="true"
@click="onAutoCreate(CircuitComponent.Diode)"
@dragstart="onDragStart($event, CircuitComponent.Diode)"
>
Diode
</button>
<button
:draggable="true"
class="btn"
@click="onAutoCreate(CircuitComponent.DCVoltageSource)"
@dragstart="onDragStart($event, CircuitComponent.DCVoltageSource)"
>
Expand Down
16 changes: 7 additions & 9 deletions src/components/MainCircuit.vue
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
<script lang="ts" setup>
import { onMounted, ref } from 'vue'
import { ref } from 'vue'
import { Background } from '@vue-flow/background'
import {
ConnectionLineType,
VueFlow,
useVueFlow,
type DefaultEdgeOptions,
type Node
} from '@vue-flow/core'
import { ConnectionLineType, type DefaultEdgeOptions, type Node, useVueFlow, VueFlow } from '@vue-flow/core'
import useDragAndDrop from '@/hooks/useDnDCircuitComponent'
import useCircuitStore from '@/stores/circuitStore'
Expand All @@ -18,9 +12,9 @@ import ACVoltage from '@/components/circuits/sources/ACVoltage.vue'
import { type ComponentData } from '@/types'
import CustomEdge from '@/CustomEdge.vue'
import { obj } from '@/stores/exampleFlowObject'
import GroundNode from '@/components/circuits/GroundNode.vue'
import InductorNode from '@/components/circuits/passive/InductorNode.vue'
import DiodeNode from '@/components/circuits/DiodeNode.vue'
type NodeTypes = 'resistor' | 'voltagesource' | 'ground'
Expand Down Expand Up @@ -100,6 +94,10 @@ onNodeClick((event) => {
<InductorNode v-bind="inductorNodeProps" />
</template>

<template #node-diode="diodeNodeProps">
<DiodeNode v-bind="diodeNodeProps" />
</template>

<template #node-voltagesource="voltagesourceNodeProps">
<VoltageSource v-bind="voltagesourceNodeProps" />
</template>
Expand Down
12 changes: 11 additions & 1 deletion src/components/PropertiesListBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,28 @@
<div v-else-if="props.selectedComponent.type == CircuitComponent.Inductor">
<InductorProperty v-bind="props.selectedComponent.data as InductorData" />
</div>
<div v-else-if="props.selectedComponent.type == CircuitComponent.Diode">
<DiodeProperty v-bind="props.selectedComponent.data as DiodeData" />
</div>
</div>
</div>
</template>

<script lang="ts" setup>
import { type Node } from '@vue-flow/core'
import { CircuitComponent, type ACVoltageSourceData, type CapacitorData, type InductorData } from '@/types'
import {
type ACVoltageSourceData,
type CapacitorData,
CircuitComponent,
type DiodeData,
type InductorData
} from '@/types'
import ResistorProperty from './properties/ResistorProperty.vue'
import CapacitorProperty from './properties/CapacitorProperty.vue'
import VoltageSourceProperty from './properties/VoltageSourceProperty.vue'
import ACVoltageSourceProperty from './properties/ACVoltageSourceProperty.vue'
import InductorProperty from '@/components/properties/InductorProperty.vue'
import DiodeProperty from '@/components/properties/DiodeProperty.vue'
interface Props {
selectedComponent: Node | null
Expand Down
56 changes: 56 additions & 0 deletions src/components/circuits/DiodeNode.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<script lang="ts" setup>
import { onMounted, ref } from 'vue'
import { Handle, type NodeProps, Position, useVueFlow } from '@vue-flow/core'
import { NodeToolbar } from '@vue-flow/node-toolbar'
import IconComponent from '@/assets/svgs/Diode.svg?component'
import type { DiodeData } from '@/types'
const props = defineProps<NodeProps<DiodeData>>()
defineOptions({
inheritAttrs: false
})
const { updateNodeData, onNodeClick, removeNodes } = useVueFlow()
onMounted(() => {
const id = props.id.split(' ')[1]
updateNodeData<DiodeData>(props.id, {
id: `D${id}`,
type: 'diode',
description: 'Diode model 1N914',
toolbarPosition: Position.Right,
toolbarVisible: false
})
})
const toolbarVisible = ref(props.data.toolbarVisible)
onNodeClick((event) => {
if (event.node.id !== props.id) return
toolbarVisible.value = !toolbarVisible.value
})
const handleRemoveNode = () => {
removeNodes(props.id, true)
}
</script>

<template>
<div class="font-mono">{{ props.data.id }}</div>
<NodeToolbar
:is-visible="toolbarVisible"
:position="data.toolbarPosition"
style="display: flex; gap: 0.5rem; align-items: center"
>
<div class="flex flex-col gap-2">
<button class="btn btn-error" @click="handleRemoveNode">x</button>
</div>
</NodeToolbar>

<IconComponent />
&#160;

<Handle :position="Position.Left" type="source" />
<Handle :position="Position.Right" type="target" />
</template>
37 changes: 37 additions & 0 deletions src/components/properties/DiodeProperty.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<template>
<!-- <div class="flex items-center justify-between mx-4">-->
<!-- <label for="input-field" class="mr-2">Diode</label>-->
<!-- <div class="relative">-->
<!-- <input-->
<!-- type="number"-->
<!-- id="input-field"-->
<!-- class="px-3 py-2 input input-bordered w-auto"-->
<!-- v-model="inductance"-->
<!-- @change="handleUpdate"-->
<!-- />-->
<!-- <span class="absolute right-2 top-1/2 transform -translate-y-1/2 text-gray-500">H</span>-->
<!-- </div>-->
<!-- </div>-->
</template>

<script lang="ts" setup>
import type { DiodeData } from '@/types'
const props = defineProps<DiodeData>()
// const inductance = ref(props.inductance)
// watch(
// () => props.inductance,
// (value) => {
// inductance.value = value
// }
// )
//
// const circuitStore = useCircuitStore()
//
// function handleUpdate() {
// const node = circuitStore.getSelectedNode()
// if (!node) return
// node.data.inductance = inductance.value
// }
</script>
25 changes: 12 additions & 13 deletions src/logic/main.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
import type { Edge, FlowExportObject, Node } from '@vue-flow/core'
import useRunStore from '@/stores/runStore'
import {
ACVoltageSource,
Capacitor,
DCVoltageSource,
Ground,
Resistor,
Component,
Inductor
} from './models'
import { CircuitComponent, type ACVoltageSourceData } from '@/types'
import { ACVoltageSource, Capacitor, Component, DCVoltageSource, Diode, Ground, Inductor, Resistor } from './models'
import { type ACVoltageSourceData, CircuitComponent } from '@/types'

function convertGraphToNetlist(circuit: FlowExportObject): string {
const nodeMap: { [nodeId: string]: Component } = {}
Expand All @@ -34,6 +26,9 @@ function convertGraphToNetlist(circuit: FlowExportObject): string {
case CircuitComponent.DCVoltageSource:
component = new DCVoltageSource(`V${++idSourceComponent}`, node.data.Dc)
break
case CircuitComponent.Diode:
component = new Diode(node.data.id)
break
case CircuitComponent.output:
component = new Ground(node.id)
break
Expand All @@ -52,16 +47,16 @@ function convertGraphToNetlist(circuit: FlowExportObject): string {
nodeMap[node.id] = component
})

// Update component connections based on edges
let edgeId = 0
circuit.edges.forEach((edge: Edge) => {
const sourceNode = nodeMap[edge.source]
const targetNode = nodeMap[edge.target]

if (targetNode instanceof Ground || sourceNode instanceof Ground) {
edge.data.id = 0
console.log(typeof edge.data)
edge.data.id = '0'
} else {
edge.data.id = ++edgeId
edge.data.id = `${++edgeId}`
}

sourceNode.neg = edge.data.id
Expand All @@ -80,6 +75,8 @@ function convertGraphToNetlist(circuit: FlowExportObject): string {
netlist.push(`${component.id} ${component.pos} ${component.neg} ${component.capacitance}`)
} else if (component instanceof Inductor) {
netlist.push(`${component.id} ${component.pos} ${component.neg} ${component.inductance}`)
} else if (component instanceof Diode) {
netlist.push(`${component.id} ${component.pos} ${component.neg} 1N914`)
} else if (component instanceof DCVoltageSource) {
netlist.push(`${component.id} ${component.pos} ${component.neg} ${component.voltage}`)
} else if (component instanceof ACVoltageSource) {
Expand Down Expand Up @@ -127,6 +124,8 @@ function handleAPI(circuit: FlowExportObject) {
const title = 'Circuit Analysis\n'
netlist = title + netlist
const exportNodes = getAnalysisType(circuit.edges)

netlist = netlist + '\n.model 1N914 D(Is=1e-14 Rs=0 N=1 Bv=1e30 Cjo=0 M=0.5 tt=0)'
netlist = netlist + `\n.END`
const mode = useRunStore().getMode()

Expand Down
9 changes: 9 additions & 0 deletions src/logic/models/Diode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Component } from '@/logic/models/Component'

export class Diode extends Component {
// TODO: diode have model

constructor(id: string) {
super(id)
}
}
5 changes: 3 additions & 2 deletions src/logic/models/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Component } from './Component'
import { Resistor, Capacitor, Inductor } from './Passive'
import { Capacitor, Inductor, Resistor } from './Passive'
import { Ground } from './Ground'
import { Diode } from './Diode'
import { ACVoltageSource, DCVoltageSource } from './Sources'

export { Component, Resistor, Capacitor, Inductor, DCVoltageSource, ACVoltageSource, Ground }
export { Component, Resistor, Capacitor, Inductor, DCVoltageSource, ACVoltageSource, Ground, Diode }
21 changes: 19 additions & 2 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Position } from '@vue-flow/core'
import { SimulationMode } from './SimulationMode'

enum CircuitComponent {
Ground = 'ground',
Expand All @@ -7,6 +8,7 @@ enum CircuitComponent {
ACVoltageSource = 'acvoltagesource',
Capacitor = 'capacitor',
Inductor = 'inductor',
Diode = 'diode',
output = 'output'
}

Expand Down Expand Up @@ -42,8 +44,23 @@ interface InductorData extends ComponentData {
inductance: number
}

export type { ComponentData, VoltageSourceData, ResistorData, ACVoltageSourceData, CapacitorData, InductorData }
interface DiodeData extends ComponentData {
IS?: string,
RS?: string,
N?: string,
BV?: string,
IBV?: string,
}

export type {
ComponentData,
VoltageSourceData,
ResistorData,
ACVoltageSourceData,
CapacitorData,
InductorData,
DiodeData
}
export { CircuitComponent }

import { SimulationMode } from './SimulationMode'
export { SimulationMode }

0 comments on commit fea5f13

Please sign in to comment.